0

I am trying to make authorization from Google on my website.

And at the step of exchanging code to access_token I obtain error:

Code was already redeemed

It happens with using django-allauth lib aswell as with using recommended by Google libs and code:

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)

When I am using test config with localhost url on my local machine, authorization works fine.

But when I use production server i got error.

In google web application I have both redirect URIs:

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

As I can see, code exahnging only once, so why it could happens and how to fix it?

user1931780
  • 446
  • 1
  • 6
  • 17
  • Seems like an expired short-lived code that needs to be refreshed. [Possible help](https://stackoverflow.com/questions/32705682/google-api-php-offline-access-invalid-grant-code-was-already-redeemed). Are you using a refresh token? – Jarad Dec 13 '17 at 17:16
  • Actually, I can't obtain refresh token, becouse `fetch_token` (where both token should come) fails. About short-live `code`: haven't found any info about that in docs. – user1931780 Dec 13 '17 at 17:22

2 Answers2

1

May be late answer, But you may find this useful https://github.com/anupy/google-oauth Need to put this library in your project. Or somebody may find useful.

Anup Yadav
  • 2,825
  • 3
  • 21
  • 30
0

I resolved it using python-social-auth app

user1931780
  • 446
  • 1
  • 6
  • 17