我正在创建一个脚本,可以使用Google-api-python-client为特定的Google Apps用户下载文件。首先,我试图获取用户帐户包含的文件列表。 我正在关注链接https://developers.google.com/drive/v2/reference/files/list
中给出的示例代码块在下面给出
用户对客户的授权
def authorize_application(request):
#setting flow to get permission and code
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI, ACCESS_TYPE)
authorize_url = flow.step1_get_authorize_url()
code = request.GET.get('code', '')
if code:
#import ipdb
#ipdb.set_trace()
#setting flow step2 to exchage code for access token
credential = flow.step2_exchange(code)
#initialising httplib2 instance and building a DriveAPI service
http = httplib2.Http()
http = credential.authorize(http)
drive_service = build('drive', 'v2', http=http)
# getting user's data
about = drive_service.about().get().execute()
user_info = about.get('user')
email = user_info.get('emailAddress')
request.session['username'] = email[0:email.index('@')]
username = request.session['username']
#import ipdb
#ipdb.set_trace()
#creating a Django user object
user, created = User.objects.get_or_create(username=username, email=email)
#saving credentials to database
if created == True:
storage = Storage(CredentialsModel, 'id', user, 'credential')
storage.put(credential)
return HttpResponseRedirect('/download/')
else:
return HttpResponseRedirect('/download/')
else:
return HttpResponseRedirect(authorize_url)
获取用户云中的文件列表
def download_file(request):
#import ipdb
#ipdb.set_trace()
username = request.session['username']
user = User.objects.get(username=username)
storage = Storage(CredentialsModel, 'id', user, 'credential')
credential = storage.get()
access_token = credential.access_token
http = httplib2.Http()
http = credential.authorize(http)
drive_service = build('drive', 'v2', http=http)
result = []
page_token = access_token
while True:
try:
param = {}
if page_token:
param['pageToken'] = page_token
files = drive_service.files().list(**param).execute()
result.extend(files['items'])
page_token = files.get('nextPageToken')
if not page_token:
break
except errors.HttpError, error:
print 'An error occured: %s' % error
break
return HttpResponse(result)
它正在保存并从数据库中检索凭据,但是它给出了错误“AccessTokenRefreshError:invalid_grant” files = drive_service.files()。list(** param).execute()of download_file()
我做错了什么?请提出正确的建议。
答案 0 :(得分:1)
(2017年2月)不确定您的问题是什么,但有几点:1)Drive API有newer version(v3),以及2)auth现在,使用Google API客户端库中的最新更新可以显着简化您使用的代码。 (确保使用pip install -U google-api-python-client
[或Python {3的pip3
]更新Python库。)
以下是一个有效的解决方案 - 告诉我它是否适合您 - 我在this blog post和this video中写道。我省略了about()
调用以获取用户信息,但我将代码分成上面相同的2个部分,但正如您所看到的,代码少得多:
用户对客户的授权
SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_id.json', SCOPES)
creds = tools.run_flow(flow, store)
获取用户云端硬盘中的文件列表(前100个文件)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
files = DRIVE.files().list().execute().get('files', [])
for f in files:
print(f['name'], f['mimeType'])
如果您想了解有关使用云端硬盘API的更多信息,请参阅我创建的一些其他资源(视频,博客文章等):
(*) - TL; DR:将纯文本文件上传到云端硬盘,导入/转换为Google文档格式,然后将该文档导出为PDF格式。上面的帖子使用Drive API v2,就像您的代码示例一样; this follow-up post描述了将其迁移到Drive API v3,这里有一个developer video结合了“穷人的转换器”帖子。