Google OAuth 2 authorization - Error: redirect_uri_mismatch

前端 未结 30 1938
遥遥无期
遥遥无期 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: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.

提交回复
热议问题