My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/ruby
#
# Authors:: sgomes@google.com (Sérgio Gomes)
# jeffy@google.com (Jeffrey Posnick)
#
# Copyright:: Copyright 2010, Google Inc. All Rights Reserved.
#
# License:: Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This library connects to Google's ClientLogin service and generates an
# AuthToken that can be used to login to the AdWords API.

require 'cgi'
require 'net/http'
require 'net/https'

module AdWords

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

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

# 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.
#
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
end
end

Change log

r56 by api.sgomes on Apr 30, 2010   Diff
Releasing 19.0.0
Go to: 
Project members, sign in to write a code review

Older revisions

r41 by api.sgomes on Oct 20, 2009   Diff
Updating library to 17.0.0. Check
ChangeLog.txt for changes.
r38 by api.sgomes on Jun 10, 2009   Diff
New non-breaking feature release:
15.1.0. Check ChangeLog.txt for
changes.
r35 by api.sgomes on May 13, 2009   Diff
Major rewrite following update to
support v200902.
See details in ChangeLog.txt, under
version 15.0.0.
All revisions of this file

File info

Size: 2600 bytes, 77 lines
Powered by Google Project Hosting