Google authentication error: invalid_request Missing required parameter: client_id

21,115

It doesn't look like your ENV-variable GOOGLE_CLIENT_ID is set, try this instead.

config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, Rails.application.secrets.google_client_id, Rails.application.secrets.google_client_secret
end

The reason for your example not working is that you don't have the ENV["GOOGLE_CLIENT_ID"] set to any value. To set ENV-variables (in Linux) you can do any of the following on the console

export GOOGLE_CLIENT_ID=my_client_id

This will however just set the variable in that session, to make this permanent you have to do edit your ~/.bash_profile

$ vi ~/.bash_proflle

And add the line above in that file.

How do I access variables in secrets.yml?

When you add information to the config/secrets.yml file they are accessed by using

Rails.application.secrets.my_key_name

I want to store my ENV-variables in a YML file

For this you can use the Figaro gem

Add the gem to your gemfile

gem "figaro"

Do bundle install

$ bundle exec figaro install

This will add the file config/application.yml (which you should add to your .gitignore)

In there you can add your ENV-variables

# config/application.yml

google_client_id: "this is my google id"
google_client_secret: "this is my client secret"

And after this you can access your client_id by just using

ENV["GOOGLE_CLIENT_ID"]
Share:
21,115
Billy Logan
Author by

Billy Logan

Updated on June 16, 2020

Comments

  • Billy Logan
    Billy Logan almost 4 years

    I'm trying to make google authentication using omniauth-google-oauth2 gem in my project and I've stumbled upon the following error when following the link localhost:3000/auth/google_oauth2/.

    enter image description here

    Why is that?

    config/initializers/omniauth.rb

    Rails.application.config.middleware.use OmniAuth::Builder do
      provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"]
    end
    

    config/routes.rb

    Rails.application.routes.draw do
      get '/auth/:provider/callback', to: 'sessions#create'
    
      root to: 'welcome#index'
    end
    

    config/secrets.yml

    development:
      secret_key_base: here_goes_my_secret_key_base
      google_client_id: 283155193283-8hnfdph0n4089iql70dh8g7428d258qc.apps.googleusercontent.com
      google_client_secret: xHnbKtackWe4D_1mrWTbRS9f
    
    test:
      secret_key_base: here_goes_my_secret_key_base
    
    production:
      secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
      google_client_id: <%= ENV["GOOGLE_CLIENT_ID"] %>
      google_client_secret: <%= ENV["GOOGLE_CLIENT_SECRET"] %>
    
  • Billy Logan
    Billy Logan over 8 years
    Thank you a lot. It solved the problem, however I don't get why this refused to work with the above example.
  • Billy Logan
    Billy Logan over 8 years
    secrets.yml in my project is in .gitignore. Maybe it could have to do with this?
  • Jacob Rastad
    Jacob Rastad over 8 years
    I added some more explanation to this :) secrets.yml should always be in .gitignore, You don't want your secret keys accessible in source control. When you deploy your app to production you should copy the secrets.yml file to the production servers.
  • Billy Logan
    Billy Logan over 8 years
    Excellent explanation. Thank you tons!