Class: DriveV3::CreateCredential

Inherits:
Object
  • Object
show all
Defined in:
lib/drive_v3/create_credential.rb

Overview

Creates a Google API credential with an access token

Class Method Summary collapse

Class Method Details

.call(credential_source, scopes, credential_factory = Google::Auth::DefaultCredentials) ⇒ Object

Creates a Google API credential with an access token

This wraps the boiler plate code into one function to make constructing a credential easy and less error prone.

Examples:

Constructing a credential from the contents of ~/.credential

credential_source = File.read(File.join(Dir.home, '.credential'))
scope = Google::Apis::DriveV3::AUTH_DRIVE
credential = DriveV3::CreateCredential.call(credential_source, scope)

Parameters:

  • credential_source (Google::Auth::*, String, IO, nil)

    may be one of four things: (1) a previously created credential that you want to reuse, (2) a credential read into a string, (3) an IO object with the credential ready to be read, or (4) if nill, the credential is read from ~/.google-api-credential.json

  • scopes (Object, Array)

    one or more scopes to access.

  • credential_factory (#make_creds) (defaults to: Google::Auth::DefaultCredentials)

    Used inject the credential_factory for tests

Returns:

  • (Object)

    a credential object with an access token



28
29
30
31
32
33
34
35
36
# File 'lib/drive_v3/create_credential.rb', line 28

def self.call(
  credential_source, scopes, credential_factory = Google::Auth::DefaultCredentials
)
  return credential_source if credential?(credential_source)

  credential_source ||= default_credential_source
  options = make_creds_options(credential_source, scopes)
  credential_factory.make_creds(options).tap(&:fetch_access_token)
end