在https://code.google.com/apis/console网站上,我已经注册了我的应用程序,设置生成的客户端ID:和客户端秘密到我的应用程序,并尝试登录谷歌。 不幸的是,我收到了错误信息:

Error: redirect_uri_mismatch
The redirect URI in the request: http://127.0.0.1:3000/auth/google_oauth2/callback did not match a registered redirect URI

scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
response_type=code
redirect_uri=http://127.0.0.1:3000/auth/google_oauth2/callback
access_type=offline
approval_prompt=force
client_id=generated_id

这条信息是什么意思,我该如何修复它? 我使用宝石omniauth-google-oauth2。


当前回答

这个答案与Mike的答案和Jeff的答案相同,都在客户端设置redirect_uri为postmessage。我想添加更多关于服务器端的信息,以及适用于此配置的特殊情况。

技术堆栈

后端

Python 3.6 Django 1.11 Django REST Framework 3.9:服务器作为API,不渲染模板,在其他地方没有做太多。 Django REST Framework JWT 1.11 Django REST Social Auth < 2.1

前端

React: 16.8.3, create-react-app版本2.1.5 react-google-login: 5.0.2

“代码”流程(专门用于谷歌OAuth2)

总结:React—>请求社交认证“代码”—>请求jwt令牌,以获得您自己的后端服务器/数据库的“登录”状态。

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. Frontend send this to backend server's JWT endpoint. POST request, with { "provider": "google-oauth2", "code": "your retrieved code here", "redirect_uri": "postmessage" } 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. 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. 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.

我的天啊,我花了6个多小时终于答对了!我想这是我第一次看到这种post - message的东西。任何使用Django + DRF + JWT + Social Auth + React组合的人都肯定会遇到这种情况。我真不敢相信,除了这里的答案,没有一篇文章提到这个。但是如果你正在使用Django + React堆栈,我真的希望这篇文章可以为你节省大量的时间。

其他回答

更新—>适用于Android应用程序

只使用:

http://localhost/oauth2callback

如果你处理自己的逻辑没有重定向链接的web应用程序

它已经得到了彻底的回答,但最近(比如,一个月前)谷歌停止接受我的URI,它将无法工作。事实上,我知道它以前做过,因为有一个用户注册了它。

无论如何,问题是常规400:redirect_uri_mismatch,但唯一的区别是,它是从https://更改为http://,和谷歌将不允许您注册http://重定向URI,因为它们是生产发布状态(而不是localhost)。

问题是在我的回调(我使用护照认证),我只做了

callbackURL: "/register/google/redirect"

读文档,他们用的是完整的URL,所以我改成了

callbackURL: "https://" + process.env.MY_URL+ "/register/google/redirect"

将https localhost添加到我接受的URI中,这样我就可以在本地进行测试,它又开始工作了。

TL;DR使用完整的URL,这样你就知道你要重定向到哪里

重定向URI(返回响应的地方)必须在api控制台中注册,错误指示您没有这样做,或者没有正确地这样做。

转到项目的控制台,在API Access下查看。您应该在那里看到您的客户端ID和客户端秘密,以及一个重定向uri列表。如果您想要的URI没有列出,单击编辑设置并将URI添加到列表中。

编辑:(来自下面评价很高的评论)请注意,更新谷歌api控制台和当前的更改可能需要一些时间。通常只有几分钟,但有时似乎更长。

2015年7月15日-上周在登录时使用这个脚本工作的登录

<script src="https://apis.google.com/js/platform.js" async defer></script>

停止工作并开始导致Error 400 with Error: redirect_uri_mismatch

然后在DETAILS部分:redirect_uri=storagerelay://…

我把它改成:

<script src="https://apis.google.com/js/client:platform.js?onload=startApp"></script>

在我的情况下,我必须检查客户端ID类型的web应用程序/已安装的应用程序。

已安装的应用程序:http://localhost[重定向uri] 在这种情况下,localhost就可以工作了

web应用程序:您需要有效的域名[重定向uri:]