Created
November 12, 2014 18:44
-
-
Save case-eee/370cd86f9664eb8cc979 to your computer and use it in GitHub Desktop.
OAuth Example w/ Google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
require 'sinatra' | |
require 'securerandom' | |
require 'json' | |
get '/' do | |
erb :index | |
end | |
get '/redirect_auth_url' do | |
client_id = ENV["CLIENT_ID"] | |
redirect "https://accounts.google.com/o/oauth2/auth?" + | |
"scope=email%20profile" + | |
"&state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome" | |
"&redirect_uri=http://localhost:9393/oauth2callback" + | |
"&response_type=code" + | |
"&client_id=#{client_id}" + | |
"&approval_prompt=force" | |
end | |
get '/oauth2callback' do | |
# p params['code'] | |
token_response = HTTParty.post("https://accounts.google.com/o/oauth2/token", | |
body: { | |
code: params[:code], | |
client_id: ENV["CLIENT_ID"], | |
redirect_uri: "http://localhost:9393/oauth2callback", | |
client_secret: ENV["CLIENT_SECRET"], | |
grant_type: "authorization_code" | |
}) | |
# p token_response['access_token'] | |
people_response = HTTParty.get("https://www.googleapis.com/plus/v1/people/me?access_token=#{token_response['access_token']}") | |
p people_response | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment