设计与发展Google OAuth2:无法根据请求确定客户端ID

时间:2018-05-11 06:49:21

标签: ruby-on-rails ruby devise google-authentication

我按照教程:Integrating Social Login with rails

的Gemfile:

gem 'devise', '~> 4.2'
gem 'omniauth-google-oauth2', '~> 0.4.1'
gem 'activerecord-session_store', '~> 1.0'

group :development do
 gem 'dotenv', '~> 2.2.1'
end

配置/初始化/ omniauth.rb

OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, '*client_id*', '*client_secret*'
end

配置/初始化/ devise.rb

Devise.setup do |config|
  config.omniauth :google_oauth2, ENV['*client_id*'], ENV['*client_secret*'], scope: 'userinfo.email,userinfo.profile',provider_ignores_state: true
end

配置/初始化/ session_store.rb

Rails.application.config.session_store :active_record_store, key: '_devise-omniauth_session'

应用/控制器/用户/ omniauth_controller.rb

  # google callback
  def google_oauth2
    @user = User.create_from_google_data(request.env['omniauth.auth'])
    if @user.persisted?
      sign_in_and_redirect @user
      set_flash_message(:notice, :success, kind: 'Google') if is_navigational_format?
    else
      flash[:error] = 'There was a problem signing you in through Google. Please register or try signing in later.'
      redirect_to new_user_registration_url
    end
  end

  def failure
    flash[:error] = 'There was a problem signing you in. Please register or try signing in later.'
    redirect_to new_user_registration_url
  end

.ENV

GOOGLE_APP_ID: *client_id*
GOOGLE_APP_SECRET: *client_secret*

Google控制台设置 enter image description here

URL:

http://localhost:3000/users/sign_in

点击google登录后,重定向链接为:

https://accounts.google.com/signin/oauth/oauthchooseaccount?client_id=*client_id*&destination=http%3A%2F%2Flocalhost%3A3000&approval_state=!ChQycF81dk1UeGg2WjRPTEdOZ18yNRIfNHhGdkhaUmowalVZOEhuU1JuY2dubXAtOXV2ZU5CWQ%E2%88%99AB8iHBUAAAAAWvaMkkzg-COYktH8xFkYhq5k9iV0uBKM&xsrfsig=AHgIfE9ctg3rKpRUm7927RhRh8pzq2rk3w&flowName=GeneralOAuthFlow

选择帐户后,该页面会重定向回到登录状态。 控制台日志如下所示:

Started GET "/users/auth/google_oauth2/callback?state=6396a69e62eea2e698b1e03dc8483005bf167272f09db8e0&code=4/AADTB7
PfvgUIUKZqtQs2r5ANRqxBvbm79WhhVdsSp-ZKsvm11OIKpOu_FlrzdHCUpRnE7vYfifM7Wu25IR_E31k" for 127.0.0.1 at 2018-05-11 12:14
:20 +0530
(google_oauth2) Callback phase initiated.
(google_oauth2) Callback phase initiated.
(google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, invalid_request: Could not determine cli
ent ID from request.
{
  "error" : "invalid_request",

  "error_description" : "Could not determine client ID from request."
}
Processing by Users::OmniauthController#failure as HTML
  Parameters: {"state"=>"6396a69e62eea2e698b1e03dc8483005bf167272f09db8e0", "code"=>"4/AADTB7PfvgUIUKZqtQs2r5ANRqxBv
bm79WhhVdsSp-ZKsvm11OIKpOu_FlrzdHCUpRnE7vYfifM7Wu25IR_E31k"}
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)

我在网上尝试了多种解决方案,但都没有。 提前致谢!

0 个答案:

没有答案