class Google::Auth::ServiceAccountCredentials

cf [Application Default Credentials](cloud.google.com/docs/authentication/production)
console (via ‘Generate new Json Key’).
from credentials from a json key file downloaded from the developer
This class allows authorizing requests for service accounts directly
OAuth access token.
Authenticates requests using Google’s Service Account credentials via an

def self.make_creds options = {}

Parameters:
  • scope (string|array|nil) -- the scope(s) to access
  • json_key_io (IO) -- an IO from which the JSON key can be read
def self.make_creds options = {}
  json_key_io, scope, target_audience = options.values_at :json_key_io, :scope, :target_audience
  raise ArgumentError, "Cannot specify both scope and target_audience" if scope && target_audience
  if json_key_io
    private_key, client_email, project_id, quota_project_id = read_json_key json_key_io
  else
    private_key = unescape ENV[CredentialsLoader::PRIVATE_KEY_VAR]
    client_email = ENV[CredentialsLoader::CLIENT_EMAIL_VAR]
    project_id = ENV[CredentialsLoader::PROJECT_ID_VAR]
    quota_project_id = nil
  end
  project_id ||= CredentialsLoader.load_gcloud_project_id
  new(token_credential_uri: TOKEN_CRED_URI,
      audience:             TOKEN_CRED_URI,
      scope:                scope,
      target_audience:      target_audience,
      issuer:               client_email,
      signing_key:          OpenSSL::PKey::RSA.new(private_key),
      project_id:           project_id,
      quota_project_id:     quota_project_id)
    .configure_connection(options)
end

def self.unescape str

enclosing quotes.
Specifically, interprets the "\n" sequence for newline, and removes
Handles certain escape sequences that sometimes appear in input.
def self.unescape str
  str = str.gsub '\n', "\n"
  str = str[1..-2] if str.start_with?('"') && str.end_with?('"')
  str
end

def apply! a_hash, opts = {}

authenticate instead.
ServiceAccountJwtHeaderCredentials instance and uses that to
If scope(s) is not set, it creates a transient

Extends the base class.
def apply! a_hash, opts = {}
  # Use the base implementation if scopes are set
  unless scope.nil? && target_audience.nil?
    super
    return
  end
  # Use the ServiceAccountJwtHeaderCredentials using the same cred values
  # if no scopes are set.
  cred_json = {
    private_key:  @signing_key.to_s,
    client_email: @issuer
  }
  alt_clz = ServiceAccountJwtHeaderCredentials
  key_io = StringIO.new MultiJson.dump(cred_json)
  alt = alt_clz.make_creds json_key_io: key_io
  alt.apply! a_hash
end

def initialize options = {}

def initialize options = {}
  @project_id = options[:project_id]
  @quota_project_id = options[:quota_project_id]
  super options
end