Skip to content

Instantly share code, notes, and snippets.

@case-eee
Created November 12, 2014 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save case-eee/370cd86f9664eb8cc979 to your computer and use it in GitHub Desktop.
Save case-eee/370cd86f9664eb8cc979 to your computer and use it in GitHub Desktop.
OAuth Example w/ Google
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