Skip to content

Instantly share code, notes, and snippets.

@mustela mustela/ga.rb
Created Sep 5, 2017

Embed
What would you like to do?
require 'sinatra'
require 'googleauth'
require 'googleauth/web_user_authorizer'
require 'googleauth/stores/redis_token_store'
require 'google/apis/analytics_v3'
require 'google/apis/webmasters_v3'
require 'redis'
client_id = ::Google::Auth::ClientId.new(
'xxxxxxxx',
'xxxxxxxx'
)
scope = [::Google::Apis::AnalyticsV3::AUTH_ANALYTICS_READONLY,
::Google::Apis::WebmastersV3::AUTH_WEBMASTERS_READONLY]
token_store = Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)
authorizer = Google::Auth::WebUserAuthorizer.new(client_id, scope, token_store, '/oauth2callback')
get('/authorize') do
# NOTE: Assumes the user is already authenticated to the app
user_id = '1'
credentials = authorizer.get_credentials(user_id, request)
if credentials.nil?
redirect(
authorizer.get_authorization_url(
login_hint: user_id,
request: request
)
)
end
# Credentials are valid, can call APIs
puts 'Success!!'
end
get('/oauth2callback') do
target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(request)
redirect target_url
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.