Google oauth 失败并出现 "Code was already redeemed" 错误

Google oauth fail with "Code was already redeemed" error

我正尝试在我的网站上从 Google 获得授权。

并且在将 code 交换为 access_token 的步骤中,我得到错误:

Code was already redeemed

它发生在使用 django-allauth 库以及使用 Google 库和代码推荐的情况下:

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)

当我在本地机器上使用本地主机 url 的测试配置时,授权工作正常。

但是当我使用生产服务器时出现错误。

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

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

正如我所见,code 只检测了一次,那么为什么会发生这种情况以及如何解决?

可能回答晚了,但您可能会发现这很有用https://github.com/anupy/google-oauth需要将此库放入您的项目中。或者有人可能会觉得有用。

我使用 python-social-auth 应用

解决了它