在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。
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>
它已经得到了彻底的回答,但最近(比如,一个月前)谷歌停止接受我的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,这样你就知道你要重定向到哪里
在任何在客户端检索授权代码的流中,例如GoogleAuth.grantOfflineAccess() API,现在您希望将代码传递到服务器,赎回它,并存储访问和刷新令牌,那么您必须使用文字字符串postmessage而不是redirect_uri。
例如,基于Ruby文档中的代码片段:
client_secrets = Google::APIClient::ClientSecrets.load('client_secrets.json')
auth_client = client_secrets.to_authorization
auth_client.update!(
:scope => 'profile https://www.googleapis.com/auth/drive.metadata.readonly',
:redirect_uri => 'postmessage' # <---- HERE
)
# Inject user's auth_code here:
auth_client.code = "4/lRCuOXzLMIzqrG4XU9RmWw8k1n3jvUgsI790Hk1s3FI"
tokens = auth_client.fetch_access_token!
# { "access_token"=>..., "expires_in"=>3587, "id_token"=>..., "refresh_token"=>..., "token_type"=>"Bearer"}
唯一提到postmessage的谷歌文档是这个老谷歌+登录文档。下面是一个截图和存档链接,因为G+即将关闭,这个链接可能会消失:
离线访问的文档页没有提到这一点是绝对不可原谅的。# FacePalm指