谷歌oauth失败了"代码已经兑现"错误

时间:2017-12-13 16:44:05

标签: python django google-oauth google-oauth2 django-allauth

我正在尝试通过我的网站向Google授权。

在将code换成access_token的步骤中,我获得了错误:

Code was already redeemed

使用django-allauth lib以及使用Google libs和代码推荐时会发生这种情况:

redirect_uri = 'http://example.com/accounts/google/login/callback/'    
config = {u'web': {u'redirect_uris': redirect_uris,
                       u'token_uri': u'https://accounts.google.com/o/oauth2/token',
                       u'auth_uri': u'https://accounts.google.com/o/oauth2/auth',
                       u'client_id': client_id,
                       u'client_secret': client_secret,
                      }
              }
    flow = google_auth_oauthlib.flow.Flow.from_client_config(
        config,
        scopes=['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email']
    )
    flow.redirect_uri = redirect_uri
    flow.fetch_token(code=code)

当我在本地计算机上使用带有localhost url的test config时,授权可以正常工作。

但是当我使用生产服务器时,我收到了错误。

在谷歌网络应用程序中,我有两个重定向URI:

http://localhost:8888/accounts/google/login/callback/
http://example.com/accounts/google/login/callback/

正如我所看到的,code只有一次,所以为什么会发生这种情况以及如何解决它?

2 个答案:

答案 0 :(得分:1)

可能是迟到的答案,但您可能会发现这个有用https://github.com/anupy/google-oauth需要将此库放在您的项目中。或者有人可能会觉得有用。

答案 1 :(得分:0)

我使用python-social-auth app

解决了这个问题