class MyBanner::GoogleAuthorization

Constants

BASE_URL

returns authorization code in browser title bar and promps user to copy the code @see developers.google.com/api-client-library/python/auth/installed-app#choosingredirecturi

Attributes

credentials_filepath[R]
scope[R]
token_filepath[R]
user_id[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/my_banner/google_authorization.rb, line 12
def initialize(options={})
  @scope = options[:scope]
  @credentials_filepath = options[:credentials_filepath]
  @token_filepath = options[:token_filepath]
  @user_id = options[:user_id] || "default"
end

Public Instance Methods

authorization_url() click to toggle source
# File lib/my_banner/google_authorization.rb, line 42
def authorization_url
  user_authorizer.get_authorization_url(base_url: BASE_URL)
end
stored_credentials() click to toggle source

@return [Google::Auth::UserRefreshCredentials] OAuth2 credentials

# File lib/my_banner/google_authorization.rb, line 26
def stored_credentials
  user_authorizer.get_credentials(user_id)
end
user_authorizer() click to toggle source
# File lib/my_banner/google_authorization.rb, line 19
def user_authorizer
  client_id = Google::Auth::ClientId.from_file(credentials_filepath) # will throw an error without credentials file
  token_store = Google::Auth::Stores::FileTokenStore.new(file: token_filepath)
  Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
end
user_provided_code() click to toggle source

prompt user for results of redirected auth flow

# File lib/my_banner/google_authorization.rb, line 36
def user_provided_code
  puts "Please visit ... \n\n #{authorization_url} \n\n ... login to your google account, get a code, paste it here, and press enter: "
  code = $stdin.gets.chomp
  return code
end
user_provided_credentials() click to toggle source

makes a request to oauth2.googleapis.com/token

# File lib/my_banner/google_authorization.rb, line 31
def user_provided_credentials
  user_authorizer.get_and_store_credentials_from_code(user_id: user_id, code: user_provided_code, base_url: BASE_URL)
end