question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Google OAuth2 not working or missing documentation

See original GitHub issue

Hi! I am trying to figure out how to prepare Google OAuth2 authentication where there are two applications (frontend & backend) with the former initiating the process and the latter handling the actual authentication.

I have tried multiple approaches but none of them worked:

Approach 1

There is an endpoint /api/auth/google that is an instance of SocialLoginView:

class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter

The frontend starts the authentication by opening a popup with url: https://accounts.google.com/o/oauth2/v2/auth and following query parameters:

client_id: [REDACTED],
scope: email,
response_type: token,
redirect_uri: [BACKEND]/api/auth/google,

Google redirects to [BACKEND]/api/auth/google/#access_token=[REDACTED]&token_type=Bearer&expires_in=3600 but the response is HTTP 405:

{
    "detail": "Method \"GET\" not allowed."
}

Unfortunately, there are a few problems with that:

  • GET is not supported in the view - it probably should be supported as HTTPS hides query params…
  • Response comes within # not query params, so presumably implicit flow is not the right one

Approach 2

Let’s use Authorization Code Grant flow. The frontend starts the authentication by opening a popup with url: https://accounts.google.com/o/oauth2/v2/auth and following query parameters:

client_id: [REDACTED],
scope: email,
response_type: code,
redirect_uri: [BACKEND]/api/auth/google,

I have adjusted the /api/auth/google view to match the “documentation” in library code:

class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter
    client_class = OAuth2Client
    callback_url = '[BACKEND]/accounts/google/login/callback' # Django-allauth

Again, this leads to GET method not allowed…

Approach 3

Let’s add some code to handle GET requests the way POSTs are handled:

    def get(self, request, *args, **kwargs):
        self.request = request
        self.serializer = self.get_serializer(
            data=self.request.query_params,
            context={'request': request}
        )
        self.serializer.is_valid(raise_exception=True)

        self.login()
        return self.get_response()

This, in turn, leads to an error:

OAuth2Error at /api/auth/google/
Error retrieving access token: b'{\n  "error" : "redirect_uri_mismatch"\n}'

Let me add, that obviously I’ve triple-checked callback URLs in Google Developers Console.

Is there any setup with django-rest-auth and django-allauth that leads to fully-fledged authentication with Google OAuth2 and 2 parties? Why is the documentation on Social Authentication so scarce?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:12
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

22reactions
rustanacexdcommented, Jan 26, 2018

I agree that docs on social auth is so scarce.

5reactions
TeoTNcommented, Feb 5, 2018

Is there anyone that could explain how the library should actually be used?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using OAuth 2.0 to Access Google APIs | Authorization
1. Obtain OAuth 2.0 credentials from the Google API Console. · 2. Obtain an access token from the Google Authorization Server. · 3....
Read more >
How to troubleshoot an allegedly missing module (google ...
I wanted to experiment with connecting to Google Calendar APIs with a service account. The example code requires google.oauth2 , which for some ......
Read more >
Setting up OAuth 2.0 - Google Cloud Platform Console Help
Go to the Google Cloud Platform Console. · From the projects list, select a project or create a new one. · If the...
Read more >
Request is 'sometimes' missing OAUTH2 token - Google Groups
It was definitely no problem on my side: as I wrote I use the java google-ads client-lib and the error only occurred irregularly...
Read more >
The Authorization Response - OAuth 2.0 Simplified
If there is something wrong with the syntax of the request, such as the redirect_uri or client_id is invalid, then it's important not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found