Google OAuth 2 authorization - Error: redirect_uri_mismatch

前端 未结 30 1928
遥遥无期
遥遥无期 2020-11-22 02:58

On the website https://code.google.com/apis/console I have registered my application, set up generated Client ID: and Client Secret to my a

相关标签:
30条回答
  • 2020-11-22 03:14

    If you use this tutorial: https://developers.google.com/identity/sign-in/web/server-side-flow then you should use "postmessage".

    In GO this fixed the problem:

    confg = &oauth2.Config{
            RedirectURL:  "postmessage",
            ClientID:   ...,
            ClientSecret: ...,
            Scopes:      ...,
            Endpoint:     google.Endpoint,
    }
    
    0 讨论(0)
  • 2020-11-22 03:14

    Don't forget to include the path after your domain and ip. In my case, I forgot:

    /oauth2callback

    0 讨论(0)
  • 2020-11-22 03:15

    In my case, my credential Application type is "Other". So I can't find Authorized redirect URIs in the credentials page. It seems appears in Application type:"Web application". But you can click the Download JSON button to get the client_secret.json file.

    Open the json file, and you can find the parameter like this: "redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]. I choose to use http://localhost and it works fine for me.

    0 讨论(0)
  • 2020-11-22 03:15

    My problem was that I had http://localhost:3000/ in the address bar and had http://127.0.0.1:3000/ in the console.developers.google.com

    0 讨论(0)
  • 2020-11-22 03:15

    I had the same issue with google sign in, I was about to pull my hairs!!! I had correctly entered my callbacks in google Credential panel at google developer console here was my redirect urls :

    https://www.example.com/signin-google

    https://www.example.com/signin-google/

    https://www.example.com/oauth2callback

    https://www.example.com/oauth2callback/

    every thing seems fine right? but it still didn't work until I added one more magical Url I added signin-google url (which is default google callback) without www and problem solved.

    take it into account (depending of you domain) you may or may not need to add both with and without www urls

    0 讨论(0)
  • 2020-11-22 03:16

    This answer is same as this Mike's answer, and Jeff's answer, both sets redirect_uri to postmessage on client side. I want to add more about the server side, and also the special circumstance applying to this configuration.

    Tech Stack

    Backend

    • Python 3.6
    • Django 1.11
    • Django REST Framework 3.9: server as API, not rendering template, not doing much elsewhere.
    • Django REST Framework JWT 1.11
    • Django REST Social Auth < 2.1

    Frontend

    • React: 16.8.3, create-react-app version 2.1.5
    • react-google-login: 5.0.2

    The "Code" Flow (Specifically for Google OAuth2)

    Summary: React --> request social auth "code" --> request jwt token to acquire "login" status in terms of your own backend server/database.

    1. Frontend (React) uses a "Google sign in button" with responseType="code" to get an authorization code. (it's not token, not access token!)
      • The google sign in button is from react-google-login mentioned above.
      • Click on the button will bring up a popup window for user to select account. After user select one and the window closes, you'll get the code from the button's callback function.
    2. Frontend send this to backend server's JWT endpoint.
      • POST request, with { "provider": "google-oauth2", "code": "your retrieved code here", "redirect_uri": "postmessage" }
    3. For my Django server I use Django REST Framework JWT + Django REST Social Auth. Django receives the code from frontend, verify it with Google's service (done for you). Once verified, it'll send the JWT (the token) back to frontend. Frontend can now harvest the token and store it somewhere.
      • All of REST_SOCIAL_OAUTH_ABSOLUTE_REDIRECT_URI, REST_SOCIAL_DOMAIN_FROM_ORIGIN and REST_SOCIAL_OAUTH_REDIRECT_URI in Django's settings.py are unnecessary. (They are constants used by Django REST Social Auth) In short, you don't have to setup anything related to redirect url in Django. The "redirect_uri": "postmessage" in React frontend suffice. This makes sense because the social auth work you have to do on your side is all Ajax-style POST request in frontend, not submitting any form whatsoever, so actually no redirection occur by default. That's why the redirect url becomes useless if you're using the code + JWT flow, and the server-side redirect url setting is not taking any effect.
    4. The Django REST Social Auth handles account creation. This means it'll check the google account email/last first name, and see if it match any account in database. If not, it'll create one for you, using the exact email & first last name. But, the username will be something like youremailprefix717e248c5b924d60 if your email is youremailprefix@example.com. It appends some random string to make a unique username. This is the default behavior, I believe you can customize it and feel free to dig into their documentation.
    5. The frontend stores that token and when it has to perform CRUD to the backend server, especially create/delete/update, if you attach the token in your Authorization header and send request to backend, Django backend will now recognize that as a login, i.e. authenticated user. Of course, if your token expire, you have to refresh it by making another request.

    Oh my goodness, I've spent more than 6 hours and finally got this right! I believe this is the 1st time I saw this postmessage thing. Anyone working on a Django + DRF + JWT + Social Auth + React combination will definitely crash into this. I can't believe none of the article out there mentions this except answers here. But I really hope this post can save you tons of time if you're using the Django + React stack.

    0 讨论(0)
提交回复
热议问题