def get_code(request):
try:
user_info_service = get_service(request.user, 'oauth2', 'v2')
user_info = user_info_service.userinfo().get().execute()
if user_info and user_info.get('id'):
request.session['cur_user'] = user_info
return redirect('/me')
else:
raise NoUserIdException()
except AccessTokenRefreshError as atrfsh:
print atrfsh
print traceback.print_tb(sys.exc_info()[2])
except:
print "This was the exception..."
print sys.exc_info()
auth_uri = FLOW.step1_get_authorize_url()
print auth_uri
return redirect(auth_uri)
def get_service(user, name, version):
storage = Storage(CredentialsModel, 'id', user, 'credential')
credential = storage.get()
http = httplib2.Http()
http = credential.authorize(http)
service = build(name, version, http=http)
return service
我不断在 user_info = user_info_service.userinfo()
.. 行收到 AccessTokenRefreshError
。
在 AccessTokenRefreshError 的 except block 中,它正在打印“invalid_grant”。
阅读 OAuth2Credentials
对象的文档后:http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.OAuth2Credentials-class.html#authorize ,我发现 get_service
中的 credential.authorize 方法显然应该自动执行访问 token 的刷新。
无法刷新访问 token ,我做错了什么?
最佳答案
这是一个解决方法!
大约每 10 次调用 tasklists.list()
时,我也会遇到同样的错误,因此该问题并非 userinfo
所独有。
我找到的唯一解决方案是将 API 调用置于重试循环中。立即重试失败,所以我在重试前加入了 sleep 。如果 sleep 时间为 45 秒,它大约有 99.5% 的时间可以正常工作。
我希望有人能发布更好的解决方案。
关于python - Google API Python 客户端 - AccessTokenRefreshError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982533/