3

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?

Paulo Fabrício
  • 319
  • 3
  • 17
  • 2
    Did you ever get a solution to this problem? Having the same issue. – YPCrumble Mar 01 '17 at 19:41
  • Unfortunatelly, no. I'm implementing Youtube sign-in manually – Paulo Fabrício Mar 01 '17 at 20:10
  • I have the same problem and could not find a solution yet. But I think I know the reason. It is probably because your social_user.extra_data does not have a refresh token. I believe if you can login for offline access you can get access_token and refresh token just like you can do it here: https://developers.google.com/oauthplayground . But in my case there is no refresh token. – yilmazhuseyin May 07 '17 at 02:04
  • Any updates on this? Same issue – Shashank Hegde Sep 21 '18 at 14:59

2 Answers2

1

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"}))
mpkasp
  • 343
  • 3
  • 10
0

As yilmazhuseyin pointed above, the issue is related to refresh token not being present. You need to pass access_type='offline' in the parameters in order for Google to return the refresh token. This can be done by adding the following in settings.py for python-social-auth in django:

SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {
    'access_type': 'offline',
}

More details can be found in Google OAuth 2.0 documentation.

shaneeb
  • 1
  • 1