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
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,
}
Don't forget to include the path after your domain and ip. In my case, I forgot:
/oauth2callback
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.
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
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
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.
create-react-app
version 2.1.5Summary: React --> request social auth "code" --> request jwt token to acquire "login" status in terms of your own backend server/database.
responseType="code"
to get an authorization code. (it's not token, not access token!)
react-google-login
mentioned above.{ "provider": "google-oauth2", "code": "your retrieved code here", "redirect_uri": "postmessage" }
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.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.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.