You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Devise集成Google OAuth2报错:无法从请求中确定客户端ID

Fixing "Could not determine client ID from request" in Rails OmniAuth Google OAuth2

Let’s walk through exactly what’s causing this error and how to fix it step by step.

Root Cause Breakdown

Looking at your configuration and error logs, the core issue is mismatched credential references across your files, plus redundant OmniAuth setup that’s creating conflicts. Let’s tackle each problem one by one.

Step 1: Fix Your .env File Format

Dotenv uses equals signs (=) instead of colons (:) for variable assignments. Update your .env to:

GOOGLE_APP_ID=your_real_google_client_id
GOOGLE_APP_SECRET=your_real_google_client_secret

Replace the placeholders with the exact values from your Google Console, and make sure there are no spaces around the equals sign.

Step 2: Remove Redundant OmniAuth Initializer

You’ve configured OmniAuth twice—once in config/initializers/omniauth.rb and once in devise.rb. For Devise + OmniAuth setups, you only need the configuration in devise.rb. Delete the entire omniauth.rb file to eliminate conflicts.

Step 3: Correct Devise Initializer Credential References

Your devise.rb has incorrect environment variable syntax (you wrapped variable names in *). Update it to use the exact keys from your .env file:

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

Step 4: Verify Session Store Setup (Optional but Critical)

Your session_store.rb looks correct, but ensure you’ve run the required migration for active record sessions:

rails generate active_record:session_migration
rails db:migrate

This ensures OAuth state tokens are stored properly, which prevents unexpected authentication failures.

Step 5: Double-Check Google Console Settings

From your screenshot, confirm these non-negotiable details:

  • Authorized Redirect URIs: Make sure it includes http://localhost:3000/users/auth/google_oauth2/callback (Google will reject callbacks if this URI doesn’t match exactly)
  • Client ID/Secret: Cross-verify that the values in your .env match exactly what’s shown in Google Console (no extra spaces or typos)

Step 6: Restart Your Rails Server

Dotenv loads environment variables on server startup, so you must restart rails s for all configuration changes to take effect.

Test the Fix

After making these changes, navigate to http://localhost:3000/users/sign_in and try the Google login again. The callback should now correctly pass your client ID to Google, resolving the "Could not determine client ID from request" error.


内容的提问来源于stack exchange,提问作者user8106529

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:16:20