Google API Python客户端 - AccessTokenRefreshError [英] Google API Python Client - AccessTokenRefreshError

查看:449
本文介绍了Google API Python客户端 - AccessTokenRefreshError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def get_code(request):
try:
user_info_service = get_service(request.user,'oauth2','v2')
user_info = user_info_service.userinfo().get()。execute()$ b $如果user_info和user_info.get('id'):
request.session ['cur_user'] = user_info
return redirect( '/ me')
else:
将AUTHOR_DIRECT ])
除外:
print这是个例外......
print sys.exc_info()
auth_uri = FLOW.step1_get_authorize_url()
print auth_uri
返回重定向(auth_uri)

def get_service(用户,名称,版本):
storage =存储(CredentialsModel,'id',用户,'凭证')
credential = storage.get()
http = httplib2.Http()
http =证书.authorize(http)
service = build(name,version,http = http)
返回服务

我一直在 user_info = user_info_service.userinfo() ...行上收到 AccessTokenRefreshError



在AccessTokenRefreshError的except块中,它打印'invalid_grant'。



阅读 OAuth2Credentials object: http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.OAuth2Credentials-class.html#authorize ,我发现显然, get_service 中的credential.authorize方法应该自动执行刷新访问令牌。



我在做什么错误,它无法刷新访问令牌?

解决方案这是一个解决方法!



我大约每10次调用一次就收到相同的错误 tasklists.list(),所以这个问题对于 userinfo 不是唯一的。

我发现的唯一解决方案是将API调用放入一个重试循环中。立即重试失败,因此在重试之前包含睡眠。 45秒的睡眠时间大约有99.5%的时间。



我希望有人可以发布更好的解决方案。


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

I keep getting an AccessTokenRefreshError on the user_info = user_info_service.userinfo().. line.

In the except block for AccessTokenRefreshError, it's printing 'invalid_grant'.

Upon reading the documentation for OAuth2Credentials object: http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.OAuth2Credentials-class.html#authorize, I found that the credential.authorize method in get_service apparently should automatically perform a refresh of the access token.

What am I doing wrong that it's not able to refresh the access token?

解决方案

This is a workaround!

I have also been getting the same error approximately once every 10 calls to tasklists.list(), so the problem is not unique to userinfo.

The only solution that I've found is to put the API call in a retry loop. Doing an immediate retry fails, so I include a sleep before retrying. With a 45 second sleep, it works approximately 99.5% of the time.

I hope that someone can post a better solution.

这篇关于Google API Python客户端 - AccessTokenRefreshError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆