class Googledriver::Authorizer

Authorizes a user to Google Drive and generates access tokens.

Constants

REDIRECT_URI

Standard redirect uri for authorization.

SCOPE

Authorization scope which only allows program to manipulate files it created.

Attributes

access_token[R]

The access token created during authorization which is needed to perform uploads to Google Drive.

token_lifetime[R]

The lifetime of an access token in seconds which dictates how often a token needs to be refreshed.

token_tob[R]

The time of birth of the current access token.

Public Class Methods

new(client_secrets_path) click to toggle source

Constructs a new Authorizer by reading client data from a file and creating a REST resource.

# File lib/googledriver/authorizer.rb, line 25
def initialize(client_secrets_path)
  @client_secrets_path = client_secrets_path
  update_client_data
  create_refresh_resource
end

Public Instance Methods

create_refresh_token() click to toggle source

Generates an authorization url for the user in order to obtain an initial refresh token.

# File lib/googledriver/authorizer.rb, line 33
def create_refresh_token
  client = OAuth2::Client.new(@client_id, @client_secret,
                              authorize_url: '/o/oauth2/auth',
                              token_url: '/o/oauth2/token',
                              site: 'https://accounts.google.com')
  url = client.auth_code.authorize_url(redirect_uri: REDIRECT_URI,
                                       scope: SCOPE, access_type: 'offline')
  puts "Open the following link and follow the onscreen instructions #{url}"
  code = gets
  token = client.auth_code.get_token(code, redirect_uri: REDIRECT_URI)
  @refresh_token = token.refresh_token
end
refresh_access_token() click to toggle source

Refreshes the access token and updates resources appropriately.

# File lib/googledriver/authorizer.rb, line 47
def refresh_access_token
  @token_tob = Time.now

  begin
    refresh = @refresh_manager.post(
      build_refresh_payload
    )
  rescue StandardError => error
    warn "#{error};  METHOD  #{__callee__};  RESOURCE  #{@refresh_manager}"
    retry
  end

  update_refresh_data(refresh)
end

Private Instance Methods

build_refresh_payload() click to toggle source
# File lib/googledriver/authorizer.rb, line 78
def build_refresh_payload
  payload = { 'refresh_token' => @refresh_token, 'client_id' => @client_id,
              'client_secret' => @client_secret,
              'grant_type' => 'refresh_token' }
  payload
end
create_refresh_resource() click to toggle source
# File lib/googledriver/authorizer.rb, line 71
def create_refresh_resource
  @refresh_manager = RestClient::Resource.new(
    'https://www.googleapis.com/oauth2/v4/token',
    headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
  )
end
update_client_data() click to toggle source
# File lib/googledriver/authorizer.rb, line 64
def update_client_data
  file_content = File.read(@client_secrets_path)
  client_data = JSON.parse(file_content)['installed']
  @client_id = client_data['client_id']
  @client_secret = client_data['client_secret']
end
update_refresh_data(refresh_data) click to toggle source
# File lib/googledriver/authorizer.rb, line 85
def update_refresh_data(refresh_data)
  processed_data = JSON.parse(refresh_data)
  @token_lifetime = processed_data['expires_in']
  @access_token = processed_data['access_token']
end