google omniauth rails 设计身份验证失败

问题描述

我想在我的 Rails 应用中添加使用 Google 的 omniauth,但重定向后出现错误

ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error,redirect_uri_mismatch: Bad Request
{
  "error": "redirect_uri_mismatch","error_description": "Bad Request"
}

我看到了有关错误重定向 uri 的错误,但没有看到“错误请求”

我在 google 控制台中的重定向 uri

http://localhost:3000/clients/auth/google_oauth2/callback

我的路线

devise_for :clients,controllers: { omniauth_callbacks: 'clients/omniauth_callbacks' }

宝石文件

gem 'omniauth-oauth2'
gem 'omniauth-google-oauth2'
gem "omniauth-linkedin-oauth2"
gem 'omniauth-rails_csrf_protection','~> 1.0'

设计

  config.omniauth :google_oauth2,ENV["GOOGLE_CLIENT_ID"],ENV["GOOGLE_CLIENT_SECRET"],access_type: "online"

我的链接

<%= link_to "Sign in with Google",client_google_oauth2_omniauth_authorize_path,method: :post %>

解决方法

检查您在其中创建凭据的 Google 开发者帐户。应该有一个名为重定向 URL 的字段,您应该在其中放置正确的 URL。此 URL 用于从使用 google 登录时打开的模式重定向用户。