Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google authentication error: invalid_request Missing required parameter: client_id

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"] %>
like image 897
Billy Logan Avatar asked Jan 15 '16 12:01

Billy Logan


Video Answer


1 Answers

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"]
like image 54
Jacob Rastad Avatar answered Sep 22 '22 07:09

Jacob Rastad