Module: AdWords::AuthToken

Defined in:
lib/adwords4r/authtoken.rb

Overview

Module providing the mechanism to obtain auth tokens for logging in to the AdWords API (>= v200902).

Constant Summary collapse

ACCOUNT_TYPE =
'GOOGLE'
AUTH_PATH =
'/accounts/ClientLogin'
SERVICE =
'adwords'

Class Method Summary collapse

Class Method Details

.get_token(email, password, hostname, port, use_ssl) ⇒ Object

Retrieve authentication token for logging in to the AdWords API.

Args:

  • email: the email address for the account being accessed

  • password: the password for the account being accessed

  • hostname: the hostname to connect to

  • port: the port to connect to

  • use_ssl: boolean indicating whether to use SSL or not

Returns: The auth token for the account (as a string).

Raises: AdWords::Error::AuthError if authentication fails.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/adwords4r/authtoken.rb', line 53

def self.get_token(email, password, hostname, port, use_ssl)
  email = CGI.escape(email)
  password = CGI.escape(password)

  http_client = Net::HTTP.new(hostname, port)
  http_client.use_ssl = use_ssl
  # Avoid annoying warning
  http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE

  data = "accountType=#{ACCOUNT_TYPE}&Email=#{email}&Passwd=#{password}" +
    "&service=#{SERVICE}"
  headers = {'Content-Type' => 'application/x-www-form-urlencoded'}

  response = http_client.post(AUTH_PATH, data, headers)

  if response.code == '200'
    return response.body[/Auth=(.*)/, 1]
  else
    raise AdWords::Error::AuthError,
        "Login failed for email %s: %s (code %d)" %
            [CGI.unescape(email), response.message, response.code]
  end
end