Google API user authentication with omniauth and device fails to find a client ID

Asked 2 years ago, Updated 2 years ago, 42 views

Prerequisites/What you want to achieve

I am producing a Twitter-type web service with rails.
I would like to be able to Google authentication with device and omniauth, but Google
If the client ID is not found, the side will scold you.
This is the page I mainly used as a reference.

Google Authentication Flow with device and omniauth - in Qiita

Login implementation from google - in Qiita

Problems/Error Messages you are experiencing

Error as shown in the picture:invalid_request
Missing required parameter: client_id appears


! [Google Authentication Link To](233d78ac7bf108c9c958f8cb1ba390f6.png)

Source Codes Affected

Gemfile

source 'https://rubygems.org'


# Bundle edge Rails installed: gem 'rails', github: 'rails/rails'
gem 'rails', '~>5.0.0'


# easy test
group:development,:test do
  gem'rspec-rails', '~>3.6'
  gem "capybara"

end
# meke user administrator
gem 'rails_admin'
# easy form create
gem'simple_form'
# Use sqlite3 as the database for Active Record
gem'sqlite 3', '~>1.3.6'
# image upload
gem'carrierwave'
# make easy restoration and login 
gem 'devise'
gem 'omniauth'
gem 'omniauth-google-oauth2'
# user admin mem
gem 'can'

group:development,:test do
  gem 'dotenv-rails'

/model/User.rb

class User<ApplicationRecord
  # Include default device modules.Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  device:database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :omniauthable

  def self.find_for_google_oauth2(auth)
  user=User.where(email:auth.info.email).first
  unless user
  user=User.create(name:auth.info.name,
                     provider —auth.provider,
                     uid —auth.uid,
                     email: auth.info.email,
                     token —auth.credentials.token,
                     password:Device.friendly_token [0,20])
  end
  user
  end
  def remember_me
    true
  end
end

/config/initializer/device.rb

#so you need to do it manually. For the users scope, it would be:
  # config.omniauth_path_prefix='/my_engine/users/auth'

  # ==>Turbolinks configuration
  # If your app is using Turbolinks, Turbolinks:: Controller needs to be included to make redirection work correctly:
  #
  # ActiveSupport.on_load(:device_failure_app)do
  #   include Turbolinks::Controller
  # end

  # ==>Configuration for:registerable

  # When set to false, does not sign a user in automatically after their password is
  # changed.Defaults to true, so a user is signed in automatically after changing a password.
  # config.sign_in_after_change_password=true
  config.omniauth: google_oauth2, Rails.application.secret.google_client_id, Rails.application.secret.google_client_secret
end

/config/initializer/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider —Google_oauth2,
    Rails.application.secrets.google_client_id,
    Rails.application.secret.google_client_secret,
    {
# I would like to get Google Calendar data after logging in, so I will send it to scope.
# It describes https://www.googleapis.com/auth/calendar.
# Also, if you set prompt and access_type to the following settings, refresh_token will be obtained.
# (Other combinations have not been tried.)
      scope: "https://www.googleapis.com/auth/userinfo.email,
              https://www.googleapis.com/auth/userinfo.profile,
              https://www.googleapis.com/auth/calendar",
      prompt: "select_account",
      access_type: "offline"
    }

end

/config/secrets.yml

#Be sure to restart your server when you modify this file.

# Your secret key is used for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!

# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
# You can use `rails secret` to generate a secure secret key.

# Make sure the secrets in this file are kept private
# If you're sharing your code publicly.

development:
  secret_key_base:77c01ff0bf353d58ef52fc73e2707886ed0b5218cd057fcb7a4404702406cc87bd6fe482f68103fc35377dadfc3fec6550df0e1fb29e941134e1f5958e561c95

test:
  secret_key_base:5124f54d8a7cb1113e4aa5ae65994ea0a0d5c73d2971c082fb9d44e1b2d2219e8c0f0f77181662084df44cd66f3e9c42db94642e57060c1026ad4f410d4f410d62c

# Do not keep production secrets in the repository,
# installed read values from the environment.
production:
  secret_key_base:<%=ENV["SECRET_KEY_BASE"]%>
#id and secret have been modified for questioning
Google_client_id: 240395658877-np5n29adf9gn3jjn88awdawdawv9gu84pl4p.apps.googleusercontent.com
google_client_secret —hAIRJiARkDFB6vGNadawdB04W

app/controllers/users/omniauth_callbacks_controller.rb

 class Users::OmniauthCallbacksController<Device::OmniauthCallbacksController
  def google_oauth2
    @user=User.find_for_google_oauth2(request.env["omniauth.auth"])

    # Check if saved
    [email protected] listed?
      flash[:notice] = I18n.t "device.omniauth_callbacks.success",:kind=>"Google"
      sign_in_and_redirect@user, :event=>:authentication
    else
      session ["device.google_data"] = request.env ["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

/config/routes

Rails.application.routes.drawdo


  mount RailsAdmin::Engine=>'/admin', as: 'rails_admin'
  root'static_pages#home'
  get '/about' = > 'static_pages #about'

    device_for:users,controllers:{
    registrations: 'users/registrations',
    sessions: "users/sessions",
    omniauth_callbacks: "users/mniauth_callbacks",
  }

  # For details on the DSL available with this file, see http://guides.rubyonrails.org/routing.html
end

Tried

I referred to the article in ↓.The method was to define the client ID in the environment variable using a gem called figaro, but the article is intended for production and I am holding it because I am not sure if I can do the same thing in the development environment.
Define client IDs in environment variables using figaro
I saw an article (I forgot the url in the article) that the duplicate redirect would give me the same error when I logged in with multiple users, and the result was the same.

Supplementary information (for example, FW/Tool Version)

Using cloud9
Rails 5.0.7.2

ruby-on-rails ruby

2022-09-30 20:25

1 Answers

There may be the following problems:

  • config/initializers/device.rb missing "s"
  • Mixed settings when using Device and not using it

The official DEADME Device section contains the following:

First define your application id and secret in config/initials/device.rb. Do not use the snippet mentied in the Usage section.
(omitted)
NOTE: If you are using this game with device with above snippet in config/initializers/device.rb then do not create config/initializers/omniauth.rb which will conflict with device configurations.

If you are using Device, you should include the required configuration in config/initializers/device.rb and do not create config/initializers/omniauth.rb.

If it hasn't been resolved yet, please try it.


2022-09-30 20:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.