在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。


当前回答

我的问题是,我在地址栏中有http://localhost:3000/,在console.developers.google.com中有http://127.0.0.1:3000/

其他回答

在任何在客户端检索授权代码的流中,例如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指

在我的例子中,我的证书应用类型是“其他”。所以我无法在凭证页中找到授权重定向uri。它似乎出现在应用程序类型:“Web应用程序”。但是您可以单击Download JSON按钮来获取client_secret。json文件。

打开json文件,你可以找到这样的参数:"redirect_uri ":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]。我选择使用http://localhost,它很适合我。

试着做这些检查:

控制台和应用程序中的Bundle ID。我更喜欢这样设置应用程序的Bundle ID: "org.peredovik.${PRODUCT_NAME:rfc1034identifier}" 检查你是否在信息选项卡添加了URL类型,只需在标识符和URL方案中输入你的Bundle ID,角色设置为编辑器 在控制台cloud.google.com“api & auth”->“同意屏幕”填写关于您的应用程序的表格。“产品名称”是必填字段。

享受:)

我有同样的问题,在我的本地机器上的端口3000的Reactjs应用程序中授权。 我已经加了lvh。me和http://lvh.me:3000分别为授权来源和授权重定向URL,如下图所示。

注意:您可以为已验证的域添加多个站点。I-e用于本地机器、登台或其他环境

如果你正在使用passport js,你正在heroku中部署你的应用程序,你需要在passport策略中添加这个设置proxy: true

passport.use(new GoogleStrategy(
    {
        clientID: keys.googleClientID,
        clientSecret: keys.googleClientSecret,
        callbackURL: '/auth/google/callback',
        proxy: true
    })

或检查您的授权url

必须是https://yourwebsite/auth/google/callback