質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

737閲覧

【Rails】omniauthとdeviseでGoogleAPIのユーザー認証時に、クライアントIDが見つからないとエラーが出てしまう

k_yusuke

総合スコア19

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/07/27 05:52

編集2019/07/27 06:11

前提・実現したいこと

railsでTwitter型WEBサービスを制作しています。
deviseとomniauthでgoogle認証できるようにしたいのですが、Google
側から「クライアントIDが見つからない」と怒られてしまいます
主に参考にしたページはこちらです

deviseとomniauthを使ったGoogle認証の流れ-in Qiita

googleからのログイン実装-in Qiita

発生している問題・エラーメッセージ

写真のようにError: invalid_request
Missing required parameter: client_idと表示されてしまいます。

![Google認証リンク先](233d78ac7bf108c9c958f8cb1ba390f6.png)

該当のソースコード

Gemfile

ruby

1source 'https://rubygems.org' 2 3 4# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 5gem 'rails', '~> 5.0.0' 6 7 8#easy test 9group :development, :test do 10 gem 'rspec-rails', '~> 3.6' 11 gem "capybara" 12 13end 14# meke user administor 15gem 'rails_admin' 16# easier form create 17gem 'simple_form' 18# Use sqlite3 as the database for Active Record 19gem 'sqlite3', '~> 1.3.6' 20# image upload 21gem 'carrierwave' 22# make easier restigation and login 23gem 'devise' 24gem 'omniauth' 25gem 'omniauth-google-oauth2' 26#user admin gem 27gem 'cancancan' 28 29group :development, :test do 30 gem 'dotenv-rails'

/model/User.rb

ruby

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable,:omniauthable 6 7 def self.find_for_google_oauth2(auth) 8 user = User.where(email: auth.info.email).first 9 unless user 10 user = User.create(name: auth.info.name, 11 provider: auth.provider, 12 uid: auth.uid, 13 email: auth.info.email, 14 token: auth.credentials.token, 15 password: Devise.friendly_token[0, 20]) 16 end 17 user 18 end 19 def remember_me 20 true 21 end 22end 23

/config/initializer/devise.rb

ruby

1 # so you need to do it manually. For the users scope, it would be: 2 # config.omniauth_path_prefix = '/my_engine/users/auth' 3 4 # ==> Turbolinks configuration 5 # If your app is using Turbolinks, Turbolinks::Controller needs to be included to make redirection work correctly: 6 # 7 # ActiveSupport.on_load(:devise_failure_app) do 8 # include Turbolinks::Controller 9 # end 10 11 # ==> Configuration for :registerable 12 13 # When set to false, does not sign a user in automatically after their password is 14 # changed. Defaults to true, so a user is signed in automatically after changing a password. 15 # config.sign_in_after_change_password = true 16 config.omniauth :google_oauth2,Rails.application.secrets.google_client_id,Rails.application.secrets.google_client_secret 17end

/config/initializer/omniauth.rb

ruby

1Rails.application.config.middleware.use OmniAuth::Builder do 2 provider :google_oauth2, 3 Rails.application.secrets.google_client_id, 4 Rails.application.secrets.google_client_secret, 5 { 6# ログイン後にGoogle Calendarのデータを取得したいので、scopeに 7# https://www.googleapis.com/auth/calendarを記述しています。 8# また、promptとaccess_typeを以下の設定にするとrefresh_tokenが得られる 9# (その他の組み合わせは試していません)。 10 scope: "https://www.googleapis.com/auth/userinfo.email, 11 https://www.googleapis.com/auth/userinfo.profile, 12 https://www.googleapis.com/auth/calendar", 13 prompt: "select_account", 14 access_type: "offline" 15 } 16 17end

/config/secrets.yml

ruby

1# Be sure to restart your server when you modify this file. 2 3# Your secret key is used for verifying the integrity of signed cookies. 4# If you change this key, all old signed cookies will become invalid! 5 6# Make sure the secret is at least 30 characters and all random, 7# no regular words or you'll be exposed to dictionary attacks. 8# You can use `rails secret` to generate a secure secret key. 9 10# Make sure the secrets in this file are kept private 11# if you're sharing your code publicly. 12 13development: 14 secret_key_base: 77c01ff0bf353d58ef52fc73e2707886ed0b5218cd057fcb7a4404702406cc87bd6fe482f68103fc35377dadfc3fec6550df0e1fb29e941134e1f5958e561c95 15 16test: 17 secret_key_base: 5124f54d8a7cb1113e4aa5ae65994ea0a0d5c73d2971c082fb9d44e1b2d2219e8c0f0f77181662084df44cd66f3e9c42db9494642ea57060c1026ad4f410d62c 18 19# Do not keep production secrets in the repository, 20# instead read values from the environment. 21production: 22 secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 23#idとシークレットは質問用に改変しています 24google_client_id: 240395658877-np5n29adf9gn3jjn88awdawdawv9gu84pl4p.apps.googleusercontent.com 25google_client_secret: hAIRJiARkDFB6vGNadawdB04W

app/controllers/users/omniauth_callbacks_controller.rb

ruby

1class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 2 def google_oauth2 3 @user = User.find_for_google_oauth2(request.env["omniauth.auth"]) 4 5 # 保存済みかどうかのチェック 6 if @user.persisted? 7 flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google" 8 sign_in_and_redirect @user, :event => :authentication 9 else 10 session["devise.google_data"] = request.env["omniauth.auth"] 11 redirect_to new_user_registration_url 12 end 13 end 14end 15

/config/routes

ruby

1Rails.application.routes.draw do 2 3 4 mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 5 root 'static_pages#home' 6 get '/about'=>'static_pages#about' 7 8 devise_for :users, controllers: { 9 registrations: 'users/registrations', 10 sessions: "users/sessions", 11 omniauth_callbacks: "users/omniauth_callbacks", 12 } 13 14 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 15end 16

試したこと

↓の記事を参考にしました。figaroというgemを使って環境変数にクライアントIDを定義するという方法だったのですが、記事は本番環境を想定していて、開発環境下の自分が同じ操作をしてよいものかわからず、保留しています。
figaroを使って環境変数にクライアントIDを定義する
複数ユーザーでログインしていると重複リダイレクトで上記のようなエラーが出るという記事(記事のurlは忘れてしまいました)を見て、ほかのユーザーをログアウトしてみたのですが、結果は同じでした。

補足情報(FW/ツールのバージョンなど)

cloud9使用
Rails 5.0.7.2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問