redirect_uri google oauth 不匹配

redirect_uri mismatch for google oauth

很抱歉这个问题已经被问过很多次了,我也看到了多个答案并与他们进行了交叉核对,但没有运气。这是我遵循的程序

1) 前往 google 开发者控制台
2) 单击创建凭据
3) Oauth 客户端 ID
4) 网络应用程序
在 authorsied redirect uri's 我给了 https://www.example.com/home

我收到客户端 ID、客户端密码的弹出窗口。我在这里添加了它们

Spree::AuthenticationMethod.where(environment: Rails.env, provider: 'google_oauth2').first_or_create do |auth_method|
    auth_method.api_key = ENV['GOOGLE_ID']
    auth_method.api_secret = ENV['GOOGLE_SECRET']
    auth_method.active = true
  end

现在我使用以下代码调用 google oauth

 <a href="https://www.example.com/users/auth/google_oauth2">
     <div>
       Sign up with google
     </div>
 </a>

编辑:

这是我遇到的错误

The redirect URI in the request, https://www.example.com/users/auth/google_oauth2/callback, does not match the ones authorized for the OAuth client. Visit https://console.developers.google.com/apis/credentials/oauthclient/490969266456-ki43pu8ejosstkuv01jadmmmt1o33the.apps.googleusercontent.com?project=490969266456 to update the authorized redirect URIs.

这是请求

Request Details
access_type=offline
scope=email
response_type=code
redirect_uri=https://www.example.com/users/auth/google_oauth2/callback
state=31ad247324b285fdf6102e931707e041770a85990050eb40
client_id=490969266456-ki43pu8ejosstkuv01jadmmmt1o33the.apps.googleuserc

看到以上信息后 我进入控制台,并将重定向 uri 更新为 https://www.example.com/users/auth/google_oauth2/callback。我仍然遇到同样的错误。

所以发生的问题是,我创建了一个没有正确重定向 uri 的帐户。我在这里

Spree::AuthenticationMethod.where(environment: Rails.env, provider: 'google_oauth2').first_or_create do |auth_method|
    auth_method.api_key = ENV['GOOGLE_ID']
    auth_method.api_secret = ENV['GOOGLE_SECRET']
    auth_method.active = true
  end

后来我知道了,我不得不在控制台中放置正确的回调 url,我创建了一个新的凭据。并在 .env 文件中更新它们。

但它似乎没有在 Spree::AuthenticationMethod 中更新它们。所有 .. 以前的键都出现了。所以我删除了那些记录并做了rails s

新密钥出现在 rails 控制台中。

另一点要记住的是

当你这样要求时 href="https://www.example.com/users/auth/google_oauth2"> google 控制台中的回调 uri 应该是这样的

https://www.example.com/users/auth/google_oauth2/callback