Class: GAAPI::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/gaapi/access_token.rb

Overview

An access token generated from a credential file provided by Google Analystics. The credential file is suitable for using in applications where humans aren’t involved, such as scheduled jobs. To obtain a credential file, follow the instructions at developers.google.com/identity/protocols/OAuth2ServiceAccount.

Instance Method Summary collapse

Constructor Details

#initialize(credential_file = nil) ⇒ AccessToken

Get a new access token. The actual token is lazily-generated, so no call is made to Google Analytics until #token is called.

Parameters:

  • credential_file (String) (defaults to: nil)

    File name of a credential file provided by Google Analytics. If ‘credential_file` is missing or `nil`, this method looks for credentials in `~/.gaapi/ga-api-key`.

Raises:

  • (StandardError)

    Throws an exception if the credential file is readable or writable by other than the current user.



16
17
18
19
20
# File 'lib/gaapi/access_token.rb', line 16

def initialize(credential_file = nil)
  @credential_file = credential_file || File.expand_path("~/.gaapi/ga-api-key")
  stat = File::Stat.new(@credential_file)
  raise "#{@credential_file} must be readable and writable only by you." if stat.world_readable? || stat.world_writable?
end

Instance Method Details

#tokenString Also known as: to_s

An access token that can be used in a request to Google Analytics Reporting API v4.

Returns:

  • (String)

    An access token.



24
25
26
# File 'lib/gaapi/access_token.rb', line 24

def token
  (@token ||= fetch_access_token)["access_token"]
end