Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Oauth2 refresh token error using python-social-auth

I'm using python-social-auth and when I try to refresh my Google Oauth2 access token I get the following error:

[2017-02-15 14:41:00,089: ERROR/MainProcess] Task tasks.tasks.test_login[169e5810-489d-4134-af8f-db3b80629fd2] raised unexpected: HTTPError(u'400 Client Error: Bad Request for url: https://accounts.google.com/o/oauth2/token',)
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 240, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 438, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/paulozullu/dev/workspaces/wopik/wopik/tasks/tasks.py", line 1928, in test_login
    social.refresh_token(strategy)
  File "/usr/local/lib/python2.7/dist-packages/social/storage/base.py", line 54, in refresh_token
    response = backend.refresh_token(token, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/social/backends/oauth.py", line 418, in refresh_token
    request = self.request(url, **request_args)
  File "/usr/local/lib/python2.7/dist-packages/social/backends/base.py", line 225, in request
    response.raise_for_status()
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 909, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
HTTPError: 400 Client Error: Bad Request for url: https://accounts.google.com/o/oauth2/token

I use the following code to refresh the access token:

from social.apps.django_app.utils import load_strategy

w_user = WUser.objects.get(auth_user=A('username','xxxx'))
social = UserSocialAuth.objects.get(user_id=w_user.auth_user.id)
strategy = load_strategy()
social.refresh_token(strategy)

Am I doing something wrong?

like image 617
Paulo Fabrício Avatar asked Aug 23 '26 02:08

Paulo Fabrício


1 Answers

I had the same problem when calling social.get_access_token(load_strategy()). If you don't want to implement Google sign-in manually, I used this workaround which forces the user to re-authenticate to refresh their tokens.

try:
    strategy = load_strategy()
    access_token = social.get_access_token(ls)
except HTTPError as e:
    return HttpResponseRedirect(reverse('social:begin', kwargs={'backend': "google-oauth2"}))
like image 178
mpkasp Avatar answered Aug 26 '26 22:08

mpkasp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!