LoginSignup
6
2

More than 5 years have passed since last update.

rubyのoauth2(gem)について2_google認証やってみる

Last updated at Posted at 2016-10-03

gem(oauth2)を使ってgoogle認証&個人情報取得してみる。

事前準備

google developerに登録してclient_id, client_secretを取得。

code

require 'oauth2'

client = OAuth2::Client.new(
    'client_id', 
    'client_secret', 
    { site: 'https://console.developers.google.com', 
      authorize_url: '/o/oauth2/auth', 
      token_url: '/o/oauth2/token'
    })

client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth2/callback')

token = client.auth_code.get_token('認証時に取得したcode', :redirect_uri => 'http://localhost:8080/oauth2/callback')

response = token.get('https://www.googleapis.com/oauth2/v1/userinfo', {access_token: token.token}).parsed

response['email'] # email取得

こんな感じ。

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2