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
我一直AccessTokenRefreshError
在user_info = user_info_service.userinfo()
..线上。
在 AccessTokenRefreshError 的异常块中,它正在打印“invalid_grant”。
在阅读OAuth2Credentials
对象的文档后:http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.OAuth2Credentials-class.html#authorize,我发现 credential.authorize 方法在get_service
显然应该自动刷新访问令牌。
我做错了什么,它无法刷新访问令牌?