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()
..行。Google API Python客戶端 - AccessTokenRefreshError
在AccessTokenRefreshError的except塊中,它打印'invalid_grant'。
在閱讀OAuth2Credentials
對象http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.OAuth2Credentials-class.html#authorize的文檔後,我發現get_service
中的credential.authorize方法顯然應該自動執行刷新訪問令牌。
我在做什麼錯,它無法刷新訪問令牌?