Skip to content

Instantly share code, notes, and snippets.

@biwakonbu
Last active August 29, 2015 14:01
Show Gist options
  • Save biwakonbu/b61b84e2096999e3e7db to your computer and use it in GitHub Desktop.
Save biwakonbu/b61b84e2096999e3e7db to your computer and use it in GitHub Desktop.
OAuth 2 による GitHub 連携認証用 URI の生成サンプル
require 'oauth2'
client_id = 'CLIENT_ID'
client_secret = 'CLIENT_SECRET'
oauth = OAuth2::Client.new(client_id,
client_secret,
{
site: 'https://api.github.com',
authorize_url: 'https://github.com/login/oauth/authorize',
token_url: 'https://github.com/login/oauth/access_token'
})
# 認証用 URI の生成
client = oauth.authorize_url(client_id: client_id, scope: 'user:email')
# 認証用 URI
p client
#=> "https://github.com/login/oauth/authorize?client_id=CLIENT_ID&scope=user%3Aemail"
# redirect to callback
# code length is 20 character
# callback URI is "https://setting.callback.url/?code=xxxxxxxxxxxxxxxxxxxx"
code = "https://setting.callback.url/?code=xxxxxxxxxxxxxxxxxxxx".sub('.*?code=', '')
token = oauth.get_code.get_token(code)
# GitHub access token
p token.token
#=> "************************************xxxx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment