Devise集成Google OAuth2报错:无法从请求中确定客户端ID
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
.envmatch 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

