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

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

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

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

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

1106閲覧

railsアプリでgoogleログインしたい

jun3030

総合スコア16

Ruby on Rails

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

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2020/08/20 13:57

前提・実現したいこと

現在作成中のアプリでgoogleログイン機能を実装したいです。

参考記事
こちらの記事を参考に実装し、ログインボタンを押すと自身のgoogleアカウントが表示され、ログインしようとすると以下のようなエラーが返ってきます。

エラー分を検索し解決を試みましたが解決できません。
お忙しい中申し訳ありませんが何かヒントを頂けないでしょうか?

よろしくお願いいたします汗

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

{ "error": { "code": 403, "message": "Legacy People API has not been used in project 446815084366 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/legacypeople.googleapis.com/overview?project=446815084366 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.", "status": "PERMISSION_DENIED", "details": [ { "@type": "type.googleapis.com/google.rpc.Help", "links": [ { "description": "Google developers console API activation", "url": "https://console.developers.google.com/apis/api/legacypeople.googleapis.com/overview?project=446815084366" } ] } ] } }

該当のソースコード

gemfile gem 'omniauth' gem 'omniauth-google-oauth2' gem 'devise'
class User < ActiveRecord::Base has_many :favorites, dependent: :destroy has_many :reviews has_many :social_profiles, dependent: :destroy # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :trackable, :omniauthable, omniauth_providers: %i[line google] enum gender: { "male": 0, "female": 1, "other": 2 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i validates :name, presence: true, length: { in: 1..30 } validates :email, presence: true, length: { maximum: 100 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: true validates :password, presence: true, length: { minimum: 6 } ,allow_nil: true validates :address, presence: true, length: { in: 5..60 } validates :gender, presence: true validates :nickname, length: { in: 1..30 }, allow_blank: true protected def social_profile(provider) social_profiles.select{ |sp| sp.provider == provider.to_s }.first end end
config/initializers/devise.rb Devise.setup do |config| require 'devise/orm/active_record' config.omniauth :line, ENV['LINE_KEY'], ENV['LINE_SECRET'] config.omniauth :google_oauth2, ENV['GOOGLE_APP_ID'], ENV['GOOGLE_APP_SECRET'], name: :google end
class SocialProfile < ActiveRecord::Base attr_accessor :access_token, :access_secret, :credentials, :name belongs_to :user def set_values(omniauth) return if provider.to_s != omniauth['provider'].to_s || uid != omniauth['uid'] credentials = omniauth['credentials'] info = omniauth['info'] self.access_token = credentials['refresh_token'] self.access_secret = credentials['secret'] self.credentials = credentials.to_json self.name = info['name'] case provider.to_s when 'google' self.nickname ||= info['email'].sub(/(.+)@gmail.com/, '\1') end self.set_values_by_raw_info(omniauth['extra']['raw_info']) end def set_values_by_raw_info(raw_info) case provider.to_s when 'google' self.url = raw_info['link'] end self.raw_info = raw_info.to_json self.save! end end
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController require 'net/http' require 'uri' require 'json' def line; basic_action end def google; basic_action; end private def basic_action @omniauth = request.env['omniauth.auth'] if @omniauth.present? @profile = SocialProfile.where(provider: @omniauth['provider'], uid: @omniauth['uid']).first if @profile @profile.set_values(@omniauth) sign_in(:user, @profile.user) else @profile = SocialProfile.new(provider: @omniauth['provider'], uid: @omniauth['uid']) email = @omniauth['info']['email'] ? @omniauth['info']['email'] : "#{@omniauth['uid']}-#{@omniauth['provider']}@example.com" @profile.user = current_user || User.create!(email: email, name: @omniauth['info']['name'], password: Devise.friendly_token[0, 20]) @profile.set_values(@omniauth) sign_in(:user, @profile.user) # redirect_to edit_user_path(@profile.user.id) and return end end flash[:success] = "ログインしました。" redirect_to root_path end end
config/routes.rb App::Application.routes.draw do devise_for :users, controllers: { sessions: 'users/sessions', passwords: 'users/passwords', registrations: 'users/registrations', omniauth_callbacks: "users/omniauth_callbacks" } end

試したこと

新しい環境を構築し、記事の通り一から実装を試みるとアッサリgoogleログインを実装できました。
ですが今のチームの開発環境ですとエラーが出ます。googleアカウントのidの情報などやredirect_urlも間違いなくあっていますし、別の環境ではこのアカウントが使えているので何か別に問題があるのかと思います。

deviseのバージョンも違うので試しましたが問題なかったです。
新しくgoogleアカウントを作り直したり、google APIがしっかり有効になっているかも確かめました。

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問