python - Google API Python 客户端 - AccessTokenRefreshError

标签 python google-api google-oauth

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/

相关文章:

python - 使用 Pandas 读取 csv 时间数据时数据类型不一致

python - python中的割线法函数

objective-c - 将 libxml2 添加到项目时出错

Codeigniter 与 google oauth2 添加主题标签 php 来重定向 ('usercp' )

Python 导入子模块时出错

python - 在 Mongodb 中存储 Numpy 或 Pandas 数据

android - 目标平台 android 与 google api

c# - 使用 Owin 和 Google 登录的 asp.net 身份 : skip registration

c# - 用于桌面应用程序的 Google 电子表格 OAuth 2.0 - 任何人都可以*简单*地使用它吗?

c# - 无法将文件上传到谷歌驱动器 - 使用 C#