Generic credentials class, used for any API version.

Methods
A
C
D
G
N
S
Attributes
[R] credentials

Hash of credentials (credential key to value)

[R] environment

The environment being used (production, sandbox)

[RW] use_mcc

Whether we’re making MCC-level requests

[RW] validate_only

Whether we’re making validate-only requests

Class Public methods
new(credentials=nil)

Constructor for AdWordsCredentials.

Args:

  • credentials: Hash of credentials (credential key to value). E.g.:
     {
      'developerToken' => 'user@domain.com++USD',
      'useragent' => 'Sample User Agent',
      'password' => 'password',
      'email' => 'user@domain.com',
      'clientEmail' => 'client_1+user@domain.com',
      'applicationToken' => 'IGNORED',
      'environment' => 'SANDBOX'
     }
    
# File lib/adwords4r/credentials.rb, line 144
    def initialize(credentials=nil)
      @credentials = {}
      @environment = nil
      @auth_token = nil
      @handlers = []
      @use_mcc = false
      @validate_only = false
      credentials = get_defaults() if credentials.nil?
      credentials.each do |key, value|
        # 'environment' shouldn't go in the credentials array, and we'll ignore
        # 'alternateUrl' to avoid errors on upgraders' apps.
        if (key =~ /^alternateUrl/) && (credentials["environment"].nil?)
          raise AdWords::Error::Error,
              "'alternateUrl' is no longer supported. Please consult the " +
              "Readme on how to use 'environment' instead."
        elsif !(key =~ /^environment/)
          @credentials[key] = value
        end
      end

      # The user agent header differs in v13 (useragent) and v2009 (userAgent).
      # Properly populate both values, and use the name of the program ($0) if
      # nothing is given.
      user_agent = 'adwords4r: %s' % (@credentials['useragent'] ||
        @credentials['userAgent'] || $0)
      @credentials['useragent'] = @credentials['userAgent'] = user_agent

      # The properties file may include the clientEmail in a clientId property.
      # clientId might be a clientCustomerId, though, so check to make sure it
      # is an email address before assigning it to clientEmail.
      # clientCustomerIds don't seem to be supported elsewhere in this client
      # library, so ignore them.
      if @credentials['clientEmail'].nil? and @credentials['clientId'] and
          @credentials['clientId'].include?('@')
        @credentials['clientEmail'] = @credentials['clientId']
      end

      # Normalize 'token' to 'developerToken'
      if @credentials['developerToken'].nil? and @credentials['token']
        @credentials['developerToken'] = @credentials['token']
        @credentials.delete('token')
      end

      # Set environment
      if credentials['environment'].nil?
        # Get default environment
        @environment = AdWords::Service.get_default_environment
      elsif !(Service.get_environments.include?(credentials['environment']))
        raise AdWords::Error::Error,
            "Unknown environment: #{credentials['environment']}"
      else
        @environment = credentials['environment']
      end

      # Fix potential problems with changing clientEmail, by forcing it to be
      # created
      @credentials['clientEmail'] = '' if @credentials['clientEmail'].nil?

      # Check for environment mismatches.
      validate_headers_for_server

      @credentials.each do |key, value|
        @handlers << Pre2009HeaderHandler.new(key, self)
      end
    end
Instance Public methods
auth_token()

Returns the authentication token used with >= v2009 requests. Generates it if there’s no valid token already generated.

Returns: The auth token (as a string).

# File lib/adwords4r/credentials.rb, line 262
    def auth_token
      generate_auth_token if @auth_token.nil?
      return @auth_token
    end
client_customer_id()

Returns the client customer ID currently being used (dependent on whether MCC-level requests are enabled or disabled)

Returns: Client customer ID if use_mcc is false, empty string otherwise

# File lib/adwords4r/credentials.rb, line 230
    def client_customer_id
      if @use_mcc
        return ''
      else
        return @credentials['clientCustomerId']
      end
    end
client_email()

Returns the client email currently being used (dependent on whether MCC-level requests are enabled or disabled)

Returns: Client email if use_mcc is false, empty string otherwise

# File lib/adwords4r/credentials.rb, line 216
    def client_email
      if @use_mcc
        return ''
      else
        return @credentials['clientEmail']
      end
    end
dup()

Overloads the ‘dup’ method for AdWordsCredentials to correctly duplicate objects of this class.

Returns: Duplicated AdWordsCredentials object

# File lib/adwords4r/credentials.rb, line 317
    def dup
      creds = @credentials.dup
      # Remove the prepended 'adwords4r: ' string before creating the duplicate
      creds['userAgent']['adwords4r: '] = ''
      creds['environment'] = @environment unless @environment.nil?
      return AdWordsCredentials.new(creds)
    end
generate_auth_token()

Generates a new authentication token used with >= v2009 requests. The generated token is stored and can later be accessed with auth_token. It should only be necessary for user code to invoke this if the first token expires.

Returns: The auth token (as a string).

# File lib/adwords4r/credentials.rb, line 275
    def generate_auth_token
      email = @credentials['email']
      password = @credentials['password']

      if email.nil?
        raise AdWords::Error::AuthError,
            'Email address not included in credentials.'
      end

      if password.nil?
        raise AdWords::Error::AuthError, 'Password not included in credentials.'
      end

      hostname, port, use_ssl = AdWords::Service.get_auth_server(@environment)
      @auth_token = AdWords::AuthToken::get_token(email, password, hostname,
          port, use_ssl)
      return @auth_token
    end
get_handlers(version, driver)

Return a list of handlers to be inserted into the driver’s handler list.

Args:

  • version: API version being used. Must be an integer.
  • driver: the driver for the service being handled

Returns: The list of handlers (subclasses of SOAP::Header::SimpleHandler)

# File lib/adwords4r/credentials.rb, line 247
    def get_handlers(version, driver)
      if version.is_a? Integer and version <= 13 then
        return @handlers
      else
        namespace = AdWords::Service.get_namespace_v2009(driver)
        return [V2009HeaderHandler.new(self, namespace, version)]
      end
    end
set_header(header, value)

Change one of the authentication headers.

Args:

  • header: the name for the header being changed.
  • value: the new value for the header.
# File lib/adwords4r/credentials.rb, line 300
    def set_header(header, value)
      # Invalidate previous auth token if necessary
      @auth_token = nil if header == 'email' or header == 'password'

      @credentials.each_key do |key|
        if key == header then
          @credentials[key] = value
        end
      end
    end