我使用gdata模块从google doc访问、上传和下载文件。我随身带着oauth密钥和秘密。现在我想切换到google drive api。在google drive api上学习和研究一下,它在身份验证方面看起来有点不同。我还下载了pydrive模块,这样我就可以启动它了。但我无法授权我的服务器端python代码使用我的oauth密钥授权/认证用户并访问我的驱动器。有谁有任何备用知道如何使用pydrive访问我的驱动器与我以前的身份验证密钥。我只需要一种简单的认证方式。
发布于 2014-05-16 22:03:47
为了使用gdata模块,我们使用这些凭证- 1>用户名和密码,或者2>消费者oauth密钥和秘密密钥。
由于您正在尝试使用oauth凭证,我认为您需要一个针对Google Drive的全域授权访问,这将帮助您通过域实现将文件上传/下载到任何用户的google drive中。
为此,您需要从Developer's Console生成服务帐户类型的新客户端ID
将下载*.p12文件。注意保存它的路径。还要注意您的服务帐户的电子邮件地址。这些将在编码时使用。
下面是python代码,为了正确运行和测试它,您必须仔细编辑SERIVE帐户私钥、something@developer.gserviceaccount.com、EMAIL_ID@YOURDOMAIN.COM的路径。
希望这能有所帮助!资源- Google Drive API
import httplib2
import pprint
import sys
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
"""Email of the Service Account"""
SERVICE_ACCOUNT_EMAIL = 'something@developer.gserviceaccount.com'
"""Path to the Service Account's Private Key file"""
SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'PATH TO SERIVE ACCOUNT PRIVATE KEY'
def createDriveService(user_email):
"""Build and returns a Drive service object authorized with the service accounts
that act on behalf of the given user.
Args:
user_email: The email of the user.
Returns:
Drive service object.
"""
f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
scope='https://www.googleapis.com/auth/drive', sub=user_email)
http = httplib2.Http()
http = credentials.authorize(http)
return build('drive', 'v2', http=http)
drive_service=createDriveService('EMAIL_ID@YOURDOMAIN.COM')
result = []
page_token = None
while True:
try:
param = {}
if page_token:
param['pageToken'] = page_token
files = drive_service.files().list().execute()
#print files
result.extend(files['items'])
page_token = files.get('nextPageToken')
if not page_token:
break
except errors.HttpError, error:
print 'An error occurred: %s' % error
break
for f in result:
print '\n\nFile: ',f.get('title')
print "\n"https://stackoverflow.com/questions/23541630
复制相似问题