Search code examples
djangoauthenticationdjango-socialauth

Django Social Auth (Google OAuth 2) - AuthCancelled even when "Allow access" is clicked


I am successfully using Django Social Auth to login users with Facebook. I'm trying to implement Google OAuth2. I have taken all the steps I know of to integrate it, but I'm getting an AuthCanceled at /complete/google-oauth2/ exception even when I click Allow access. Here's what I did to integrate it:

  • Went to the Google API console, registered my application and got my Client ID and Client Secret.
  • I added the correct return url http://mysite/complete/google-oauth2, before I did that I got "unauthorized redirect url" from Google.
  • In my settings.py I have

    GOOGLE_OAUTH2_CLIENT_ID = '*****'
    GOOGLE_OAUTH2_CLIENT_SECRET = '*****'
    
    AUTHENTICATION_BACKENDS = (
        'social_auth.backends.google.GoogleOAuth2Backend',
        'social_auth.backends.facebook.FacebookBackend',
        'django.contrib.auth.backends.ModelBackend',
    )
    
  • I guess I have the other relevant settings right, as Facebook login works, and I'm able to show the window, view my app name in the Google authorization window, etc.

Whan am I doing wrong? How can I fix/debug that AuthCancelled exception?


Solution

  • The problem appears to be caused by a strange Google behavior: when I initially created my client ID, google gave me this client ID: 193271111225.apps.googleusercontent.com. After a conversation with the library author, he told me that his ids were much longer, so I created a new client ID with the exact same settings. The new ID generated was 193271111225-cvltnldi4hh5lmo784v2ir451b3rij7e.apps.googleusercontent.com and with it, it worked. Both IDs look the same in the console, but only the latter works.