Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get 'sign in with Google' to work using 'dj_rest_auth' #374

Closed
Vipulkushwaha opened this issue Feb 16, 2022 · 2 comments
Closed

Comments

@Vipulkushwaha
Copy link

I'm trying to implement Google sign in using DRF and dj_rest_auth.

I've set up 'django-allauth' with Google as provider and the sign in process works in the web browser.

I need to connect an android app with my backend. I've created API endpoints which will require authentication.

According to the docs, code is required in order to complete authentication and receive the token.

After doing some research, I found that code required by dj_rest_auth can be obtained by visiting:

https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=<YOUR CALLBACK URL>&prompt=consent&response_type=code&client_id=<YOUR CLIENT ID>&scope=openid%20email&access_type=offline

However, even after passing code returned in the query param (after decoding from URL format), the following error is shown:

Error retrieving access token: b'{\n  "error": "invalid_grant",\n  "error_description": "Bad Request"\n}'

To see if I can log in with a recent access token, I signed in with my Google account from the homepage , copied the access token from the admin section and submitted it to the endpoint http://localhost:8000/dj-rest-auth/google/. I was able to receive the auth token generated by dj_rest_auth.

I need help in getting the auth token by providing code in the post request.

My code:

# urls.py
...
path('dj-rest-auth/', include('dj_rest_auth.urls')),
path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
path('dj-rest-auth/google/', home.GoogleLogin.as_view(), name='google_login'),
...

# views.py

from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from dj_rest_auth.registration.views import SocialLoginView

class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter
    callback_url = 'http://localhost:8000/accounts/google/login/callback/'
    client_class = OAuth2Client
...

References:

Google Social Authentication with dj-rest-auth #220

Minimal example for SPA implementation of social login #147

Please help me.

@YZ775
Copy link

YZ775 commented Mar 16, 2022

Hello.

I've tested Google Login with the same environment you pasted.

I found that the error occurs in the situation as follows.

  1. When the code is expired. (expiration is a few minutes)
  2. When callback_url does not match <YOUR CALLBACK URL> and OAuth setting in GoogleCloudConsole.

In addition, callback_url should be set as the front-end's callback URL, not a Django's URL.
So if you host front-end in localhost:3000 and endpoint is /callback, the callback_url should be
callback_url = "localhost:3000/callback"

Can you check and try this?
Thank you.

@Vipulkushwaha
Copy link
Author

Hello.

I've tested Google Login with the same environment you pasted.

I found that the error occurs in the situation as follows.

  1. When the code is expired. (expiration is a few minutes)
  2. When callback_url does not match <YOUR CALLBACK URL> and OAuth setting in GoogleCloudConsole.

In addition, callback_url should be set as the front-end's callback URL, not a Django's URL. So if you host front-end in localhost:3000 and endpoint is /callback, the callback_url should be callback_url = "localhost:3000/callback"

Can you check and try this? Thank you.

Thank you @YZ775 for explaining. I've tried and it works. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants