Class: GoogleSpreadsheets::Auth::ServiceAccountsAccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/google_spreadsheets/auth/service_accounts_access_token.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ServiceAccountsAccessToken

Returns a new instance of ServiceAccountsAccessToken.



7
8
9
10
11
12
13
14
# File 'lib/google_spreadsheets/auth/service_accounts_access_token.rb', line 7

def initialize(options = {})
  @options = options.reverse_merge(
    application_name: 'Ruby',
    token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
    audience:             'https://accounts.google.com/o/oauth2/token',
    scope:                'https://spreadsheets.google.com/feeds/'
  )
end

Instance Method Details

#call(connection) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/google_spreadsheets/auth/service_accounts_access_token.rb', line 16

def call(connection)
  # cf. http://d.hatena.ne.jp/sugyan/20130112/1357996092
  @client ||= Google::APIClient.new(application_name: @options[:application_name]).tap do |c|
    c.authorization = Signet::OAuth2::Client.new(
      token_credential_uri: @options[:token_credential_uri],
      audience:             @options[:audience],
      scope:                @options[:scope],
      issuer:               @options[:client_email],
      signing_key:          OpenSSL::PKey::RSA.new(@options[:private_key_pem])
    )
    c.authorization.fetch_access_token!
  end
  if @client.authorization.expired?
    @client.authorization.refresh!
  end
  @client.authorization.access_token
end