iMerica / dj-rest-auth

Authentication for Django Rest Framework
https://dj-rest-auth.readthedocs.io/en/latest/index.html
MIT License
1.64k stars 306 forks source link

Google OAuth Redirect Uri Missmatch error, but urls seem to be configured correctly #458

Closed adrenaline681 closed 9 months ago

adrenaline681 commented 1 year ago

1) On my frontend (ReactJs) I'm login using Google and getting the "code" using the Auth Code Flow which looks something like this:

{
    "code": "4/0AfteXvtM-2XB5q-cTe-l-oYTkkRg-3QZf8V44cEvrRvCpPR9gSXhn76dF-oU2SPxDNvSpw",
    "scope": "email profile openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email",
    "authuser": "0",
    "prompt": "consent"
}

2) Then I'm sending this in the body of a POST request to my DRF API which lands on this view:

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

3) In my Google Console I have the same callback URL setup under OAuth 2.0 Client IDs: image

4) But I keep getting the same error:

Error retrieving access token: {
    "error": "redirect_uri_mismatch",
    "error_description": "Bad Request"
}
Korben11 commented 1 year ago

Having the same issue, did you find the cause?

adrenaline681 commented 1 year ago

No, I ended up using Implicit flow instead since I only needed to get user information on login.

class GoogleLoginView(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter
adrenaline681 commented 9 months ago

I'm leaving the solution that helped me. I've lost so many hours maybe this can help someone else save some time.

The trick was to set the callback_url to postmessage

class GoogleLoginView(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter
    callback_url = 'postmessage'  
    client_class = OAuth2Client