google-dfp-api 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +5 -0
- data/README +8 -8
- data/dfp_api.yml +1 -16
- data/examples/{v201203/common/oauth_handling.rb → v201211/common/oauth2_jwt_handling.rb} +23 -43
- data/examples/v201211/publisher_query_language_service/get_all_cities.rb +30 -16
- data/lib/dfp_api.rb +4 -2
- data/lib/dfp_api/version.rb +2 -2
- metadata +5 -9
- data/examples/v201204/common/oauth_handling.rb +0 -131
- data/examples/v201206/common/oauth_handling.rb +0 -131
- data/examples/v201208/common/oauth_handling.rb +0 -131
- data/examples/v201211/common/oauth_handling.rb +0 -131
data/ChangeLog
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
0.6.0:
|
2
|
+
- Updated PQL example with paging.
|
3
|
+
- Require google-ads-common 0.9.0 or later from now on.
|
4
|
+
- Added support for AOuth2.0 JWT, removed OAuth1.0a support.
|
5
|
+
|
1
6
|
0.5.0:
|
2
7
|
- Added support and examples for v201211.
|
3
8
|
- Removed support for deprecated v201108, v201111, v201201.
|
data/README
CHANGED
@@ -90,20 +90,20 @@ or add it to the bash configuration file:
|
|
90
90
|
$ echo 'export RUBYOPT="rubygems"' >> ~/.bashrc
|
91
91
|
|
92
92
|
|
93
|
-
== Using
|
93
|
+
== Using OAuth2.0
|
94
94
|
|
95
|
-
It is recommended to use
|
96
|
-
email/password combination.
|
97
|
-
user. In order to use OAuth you just need to:
|
95
|
+
It is recommended to use OAuth2.0 authentication method instead of ClientLogin
|
96
|
+
(explicit email/password combination). OAuth2.0 support is almost transparent to
|
97
|
+
the library user. In order to use OAuth you just need to:
|
98
98
|
|
99
|
-
- set authentication method to '
|
99
|
+
- set authentication method to 'OAuth2' in the configuration;
|
100
100
|
|
101
|
-
- include
|
101
|
+
- include oauth2_client_id and oauth2_client_secret;
|
102
102
|
|
103
|
-
- handle
|
103
|
+
- handle OAuth2VerificationRequired error and pass verification code to the
|
104
104
|
library.
|
105
105
|
|
106
|
-
See 'dfp_api.yml' configuration file and 'common/
|
106
|
+
See 'dfp_api.yml' configuration file and 'common/oauth2_handling.rb' example for
|
107
107
|
more details.
|
108
108
|
|
109
109
|
== Accessing services
|
data/dfp_api.yml
CHANGED
@@ -4,8 +4,7 @@
|
|
4
4
|
# Detailed descriptions of these properties can be found at:
|
5
5
|
# https://developers.google.com/doubleclick-publishers/docs/soap_xml
|
6
6
|
:authentication:
|
7
|
-
# Authentication method, methods currently supported:
|
8
|
-
# OAuth2, OAuth, ClientLogin.
|
7
|
+
# Authentication method, methods currently supported: OAuth2, ClientLogin.
|
9
8
|
:method: ClientLogin
|
10
9
|
|
11
10
|
# Auth parameters for OAuth2.0 method.
|
@@ -20,20 +19,6 @@
|
|
20
19
|
#:oauth2_access_type: INSERT_OAUTH2_ACCESS_TYPE_HERE
|
21
20
|
#:oauth2_approval_prompt: INSERT_OAUTH2_APPROVAL_PROMPT_HERE
|
22
21
|
|
23
|
-
# Auth parameters for OAuth1.0a method.
|
24
|
-
# NOTE: OAuth1.0a method is deprecated, use OAuth2.0 instead.
|
25
|
-
# Set the OAuth consumer key and secret. Anonymous values can be used for
|
26
|
-
# testing, and real values can be obtained by registering your application:
|
27
|
-
# https://developers.google.com/accounts/docs/RegistrationForWebAppsAuto
|
28
|
-
#:oauth_consumer_key: anonymous
|
29
|
-
#:oauth_consumer_secret: anonymous
|
30
|
-
# If you manage or store access token manually, you can specify it here.
|
31
|
-
#:oauth_token: INSERT_OAUTH_TOKEN_HERE
|
32
|
-
# If you need to change signature method, specify it here.
|
33
|
-
#:oauth_signature_method: HMAC-SHA1
|
34
|
-
# Token secret for HMAC-SHA1 method.
|
35
|
-
#:oauth_token_secret: INSERT_OAUTH_TOKEN_SECRET_HERE
|
36
|
-
|
37
22
|
# Auth parameters for ClientLogin method.
|
38
23
|
:password: INSERT_PASSWORD_HERE
|
39
24
|
:email: INSERT_EMAIL_HERE
|
@@ -1,8 +1,9 @@
|
|
1
|
-
#!/usr/bin/ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Encoding: utf-8
|
2
3
|
#
|
3
4
|
# Author:: api.dklimkin@gmail.com (Danial Klimkin)
|
4
5
|
#
|
5
|
-
# Copyright:: Copyright
|
6
|
+
# Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
|
6
7
|
#
|
7
8
|
# License:: Licensed under the Apache License, Version 2.0 (the "License");
|
8
9
|
# you may not use this file except in compliance with the License.
|
@@ -17,18 +18,17 @@
|
|
17
18
|
# See the License for the specific language governing permissions and
|
18
19
|
# limitations under the License.
|
19
20
|
#
|
20
|
-
# This example shows how to use
|
21
|
-
#
|
21
|
+
# This example shows how to use OAuth2.0 authorization method with JWT (Service
|
22
|
+
# Account).
|
22
23
|
#
|
23
24
|
# Tags: UserService.getUsersByStatement
|
24
25
|
|
25
26
|
require 'dfp_api'
|
26
27
|
|
27
|
-
API_VERSION = :
|
28
|
+
API_VERSION = :v201211
|
28
29
|
PAGE_SIZE = 500
|
29
|
-
MAX_RETRIES = 3
|
30
30
|
|
31
|
-
def
|
31
|
+
def oauth2_jwt_handling()
|
32
32
|
# Get DfpApi instance and load configuration from ~/dfp_api.yml.
|
33
33
|
dfp = DfpApi::Api.new
|
34
34
|
|
@@ -36,14 +36,19 @@ def oauth_handling()
|
|
36
36
|
# the configuration file or provide your own logger:
|
37
37
|
# dfp.logger = Logger.new('dfp_xml.log')
|
38
38
|
|
39
|
-
#
|
40
|
-
#
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
39
|
+
# Option 1: provide key filename as authentication -> oauth2_keyfile in the
|
40
|
+
# configuration file. No additional code is necessary.
|
41
|
+
# To provide a file name at runtime, use authorize:
|
42
|
+
# dfp.authorize({:oauth2_keyfile => key_filename})
|
43
|
+
|
44
|
+
# Option 2: retrieve key manually and create OpenSSL::PKCS12 object.
|
45
|
+
# key_filename = 'INSERT_FILENAME_HERE'
|
46
|
+
# key_secret = 'INSERT_SECRET_HERE'
|
47
|
+
# key_file_data = File.read(key_filename)
|
48
|
+
# key = OpenSSL::PKCS12.new(key_file_data, key_secret).key
|
49
|
+
# dfp.authorize({:oauth2_key => key})
|
50
|
+
|
51
|
+
# Now you can make API calls.
|
47
52
|
|
48
53
|
# Get the UserService.
|
49
54
|
user_service = dfp.service(:UserService, API_VERSION)
|
@@ -51,30 +56,13 @@ def oauth_handling()
|
|
51
56
|
# Define initial values.
|
52
57
|
offset = 0
|
53
58
|
page = Hash.new
|
54
|
-
retry_count = 0
|
55
59
|
|
56
60
|
begin
|
57
61
|
# Create statement for one page with current offset.
|
58
62
|
statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
|
59
63
|
|
60
|
-
|
61
|
-
|
62
|
-
page = user_service.get_users_by_statement(statement)
|
63
|
-
|
64
|
-
# The second way to do OAuth authentication is to make a request and catch
|
65
|
-
# the OAuthVerificationRequired exception. Add the verification code to the
|
66
|
-
# credentials once acquired.
|
67
|
-
rescue AdsCommon::Errors::OAuthVerificationRequired => e
|
68
|
-
if retry_count < MAX_RETRIES
|
69
|
-
verification_code = get_verification_code(e.oauth_url)
|
70
|
-
dfp.credential_handler.set_credential(
|
71
|
-
:oauth_verification_code, verification_code)
|
72
|
-
retry_count += 1
|
73
|
-
retry
|
74
|
-
else
|
75
|
-
raise AdsCommon::Errors::AuthError, 'Failed to authenticate.'
|
76
|
-
end
|
77
|
-
end
|
64
|
+
# Get users by statement.
|
65
|
+
page = user_service.get_users_by_statement(statement)
|
78
66
|
|
79
67
|
if page[:results]
|
80
68
|
# Increase query offset by page size.
|
@@ -97,17 +85,9 @@ def oauth_handling()
|
|
97
85
|
end
|
98
86
|
end
|
99
87
|
|
100
|
-
# Misc util to get the verification code from the console.
|
101
|
-
def get_verification_code(url)
|
102
|
-
puts "Hit Auth error, please navigate to URL:\n\t%s" % url
|
103
|
-
print 'Log in and type the verification code: '
|
104
|
-
verification_code = gets.chomp
|
105
|
-
return verification_code
|
106
|
-
end
|
107
|
-
|
108
88
|
if __FILE__ == $0
|
109
89
|
begin
|
110
|
-
|
90
|
+
oauth2_jwt_handling()
|
111
91
|
|
112
92
|
# HTTP errors.
|
113
93
|
rescue AdsCommon::Errors::HttpError => e
|
@@ -30,6 +30,7 @@ require 'dfp_api'
|
|
30
30
|
API_VERSION = :v201211
|
31
31
|
# A string to separate columns in output. Use "," to get CSV.
|
32
32
|
COLUMN_SEPARATOR = "\t"
|
33
|
+
PAGE_SIZE = 500
|
33
34
|
|
34
35
|
def get_all_cities()
|
35
36
|
# Get DfpApi instance and load configuration from ~/dfp_api.yml.
|
@@ -42,29 +43,42 @@ def get_all_cities()
|
|
42
43
|
# Get the PublisherQueryLanguageService.
|
43
44
|
pql_service = dfp.service(:PublisherQueryLanguageService, API_VERSION)
|
44
45
|
|
45
|
-
# Create statement to select all targetable cities.
|
46
|
-
#
|
47
|
-
|
48
|
-
|
46
|
+
# Create a statement to select all targetable cities. For criteria that do not
|
47
|
+
# have a "targetable" property, the predicate may be left off.
|
48
|
+
statement_text =
|
49
|
+
'SELECT * FROM City WHERE targetable = true LIMIT %d' % PAGE_SIZE
|
49
50
|
|
50
|
-
#
|
51
|
-
result_set =
|
51
|
+
# Set initial values for paging.
|
52
|
+
offset, result_set, all_rows = 0, nil, 0
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
# Get all cities with paging.
|
55
|
+
begin
|
56
|
+
# Create statement for a page.
|
57
|
+
statement = {
|
58
|
+
:query => statement_text + ' OFFSET %d' % offset
|
59
|
+
}
|
60
|
+
result_set = pql_service.select(statement)
|
61
|
+
|
62
|
+
if result_set
|
63
|
+
# Print out columns header.
|
64
|
+
columns = result_set[:column_types].collect {|col| col[:label_name]}
|
65
|
+
puts columns.join(COLUMN_SEPARATOR)
|
57
66
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
67
|
+
# Print out every row.
|
68
|
+
result_set[:rows].each do |row_set|
|
69
|
+
row = row_set[:values].collect {|item| item[:value]}
|
70
|
+
puts row.join(COLUMN_SEPARATOR)
|
71
|
+
end
|
62
72
|
end
|
63
|
-
|
73
|
+
|
74
|
+
# Update the counters.
|
75
|
+
offset += PAGE_SIZE
|
76
|
+
all_rows += result_set[:rows].size
|
77
|
+
end while result_set[:rows].size == PAGE_SIZE
|
64
78
|
|
65
79
|
# Print a footer.
|
66
80
|
if result_set[:rows]
|
67
|
-
puts "Total number of rows found: %d" %
|
81
|
+
puts "Total number of rows found: %d" % all_rows
|
68
82
|
end
|
69
83
|
end
|
70
84
|
|
data/lib/dfp_api.rb
CHANGED
@@ -51,8 +51,10 @@ module DfpApi
|
|
51
51
|
def soap_header_handler(auth_handler, version, header_ns, default_ns)
|
52
52
|
auth_method = @config.read('authentication.method', :CLIENTLOGIN)
|
53
53
|
handler_class = case auth_method
|
54
|
-
when :CLIENTLOGIN
|
55
|
-
|
54
|
+
when :CLIENTLOGIN
|
55
|
+
DfpApi::ClientLoginHeaderHandler
|
56
|
+
when :OAUTH2, :OAUTH2_JWT
|
57
|
+
AdsCommon::SavonHeaders::OAuthHeaderHandler
|
56
58
|
else
|
57
59
|
raise AdsCommon::Errors::AuthError,
|
58
60
|
"Unknown auth method: %s" % auth_method
|
data/lib/dfp_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-dfp-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: google-ads-common
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0.
|
21
|
+
version: 0.9.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.
|
29
|
+
version: 0.9.0
|
30
30
|
description: google-dfp-api is a DFP API client library for Ruby
|
31
31
|
email:
|
32
32
|
- api.dklimkin@gmail.com
|
@@ -35,7 +35,6 @@ extensions: []
|
|
35
35
|
extra_rdoc_files: []
|
36
36
|
files:
|
37
37
|
- examples/v201204/common/error_handling.rb
|
38
|
-
- examples/v201204/common/oauth_handling.rb
|
39
38
|
- examples/v201204/common/oauth2_handling.rb
|
40
39
|
- examples/v201204/suggested_ad_unit_service/get_all_suggested_ad_units.rb
|
41
40
|
- examples/v201204/suggested_ad_unit_service/approve_all_suggested_ad_units.rb
|
@@ -154,7 +153,6 @@ files:
|
|
154
153
|
- examples/v201204/user_team_association_service/get_user_team_associations_by_statement.rb
|
155
154
|
- examples/v201204/user_team_association_service/get_user_team_association.rb
|
156
155
|
- examples/v201208/common/error_handling.rb
|
157
|
-
- examples/v201208/common/oauth_handling.rb
|
158
156
|
- examples/v201208/common/oauth2_handling.rb
|
159
157
|
- examples/v201208/suggested_ad_unit_service/get_all_suggested_ad_units.rb
|
160
158
|
- examples/v201208/suggested_ad_unit_service/approve_all_suggested_ad_units.rb
|
@@ -279,7 +277,6 @@ files:
|
|
279
277
|
- examples/v201208/user_team_association_service/get_user_team_associations_by_statement.rb
|
280
278
|
- examples/v201208/user_team_association_service/get_user_team_association.rb
|
281
279
|
- examples/v201203/common/error_handling.rb
|
282
|
-
- examples/v201203/common/oauth_handling.rb
|
283
280
|
- examples/v201203/suggested_ad_unit_service/get_all_suggested_ad_units.rb
|
284
281
|
- examples/v201203/suggested_ad_unit_service/approve_all_suggested_ad_units.rb
|
285
282
|
- examples/v201203/suggested_ad_unit_service/get_suggested_ad_unit.rb
|
@@ -384,7 +381,6 @@ files:
|
|
384
381
|
- examples/v201203/line_item_service/get_line_items_by_statement.rb
|
385
382
|
- examples/v201203/line_item_service/get_line_item.rb
|
386
383
|
- examples/v201206/common/error_handling.rb
|
387
|
-
- examples/v201206/common/oauth_handling.rb
|
388
384
|
- examples/v201206/common/oauth2_handling.rb
|
389
385
|
- examples/v201206/suggested_ad_unit_service/get_all_suggested_ad_units.rb
|
390
386
|
- examples/v201206/suggested_ad_unit_service/approve_all_suggested_ad_units.rb
|
@@ -506,8 +502,8 @@ files:
|
|
506
502
|
- examples/v201206/user_team_association_service/update_user_team_associations.rb
|
507
503
|
- examples/v201206/user_team_association_service/get_user_team_associations_by_statement.rb
|
508
504
|
- examples/v201206/user_team_association_service/get_user_team_association.rb
|
505
|
+
- examples/v201211/common/oauth2_jwt_handling.rb
|
509
506
|
- examples/v201211/common/error_handling.rb
|
510
|
-
- examples/v201211/common/oauth_handling.rb
|
511
507
|
- examples/v201211/common/oauth2_handling.rb
|
512
508
|
- examples/v201211/creative_wrapper_service/create_creative_wrappers.rb
|
513
509
|
- examples/v201211/creative_wrapper_service/update_creative_wrappers.rb
|
@@ -1,131 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# Encoding: utf-8
|
3
|
-
#
|
4
|
-
# Author:: api.dklimkin@gmail.com (Danial Klimkin)
|
5
|
-
#
|
6
|
-
# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
|
7
|
-
#
|
8
|
-
# License:: Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
-
# you may not use this file except in compliance with the License.
|
10
|
-
# You may obtain a copy of the License at
|
11
|
-
#
|
12
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
13
|
-
#
|
14
|
-
# Unless required by applicable law or agreed to in writing, software
|
15
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
17
|
-
# implied.
|
18
|
-
# See the License for the specific language governing permissions and
|
19
|
-
# limitations under the License.
|
20
|
-
#
|
21
|
-
# This example shows how to use OAuth1.0a authorization method. It is designed
|
22
|
-
# to be run from console and requires user input.
|
23
|
-
#
|
24
|
-
# NOTE: OAuth1.0a authorization method is deprecated, use OAuth2.0 instead.
|
25
|
-
# See oauth2_handling.rb for an example.
|
26
|
-
#
|
27
|
-
# Tags: UserService.getUsersByStatement
|
28
|
-
|
29
|
-
require 'dfp_api'
|
30
|
-
|
31
|
-
API_VERSION = :v201204
|
32
|
-
PAGE_SIZE = 500
|
33
|
-
MAX_RETRIES = 3
|
34
|
-
|
35
|
-
def oauth_handling()
|
36
|
-
# Get DfpApi instance and load configuration from ~/dfp_api.yml.
|
37
|
-
dfp = DfpApi::Api.new
|
38
|
-
|
39
|
-
# To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
|
40
|
-
# the configuration file or provide your own logger:
|
41
|
-
# dfp.logger = Logger.new('dfp_xml.log')
|
42
|
-
|
43
|
-
# Forcing authorization. A callback URL and other parameters could be
|
44
|
-
# specified as parameter for OAuth method.
|
45
|
-
token = dfp.authorize({:oauth_callback => 'oob'}) do |oauth_url|
|
46
|
-
# For command-line we ask user to go to URL and type in code.
|
47
|
-
verification_code = get_verification_code(oauth_url)
|
48
|
-
# Return verification code from the block.
|
49
|
-
(verification_code.empty?) ? nil : verification_code
|
50
|
-
end
|
51
|
-
|
52
|
-
# Get the UserService.
|
53
|
-
user_service = dfp.service(:UserService, API_VERSION)
|
54
|
-
|
55
|
-
# Define initial values.
|
56
|
-
offset = 0
|
57
|
-
page = Hash.new
|
58
|
-
retry_count = 0
|
59
|
-
|
60
|
-
begin
|
61
|
-
# Create statement for one page with current offset.
|
62
|
-
statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
|
63
|
-
|
64
|
-
begin
|
65
|
-
# Get users by statement.
|
66
|
-
page = user_service.get_users_by_statement(statement)
|
67
|
-
|
68
|
-
# The second way to do OAuth authentication is to make a request and catch
|
69
|
-
# the OAuthVerificationRequired exception. Add the verification code to the
|
70
|
-
# credentials once acquired.
|
71
|
-
rescue AdsCommon::Errors::OAuthVerificationRequired => e
|
72
|
-
if retry_count < MAX_RETRIES
|
73
|
-
verification_code = get_verification_code(e.oauth_url)
|
74
|
-
dfp.credential_handler.set_credential(
|
75
|
-
:oauth_verification_code, verification_code)
|
76
|
-
retry_count += 1
|
77
|
-
retry
|
78
|
-
else
|
79
|
-
raise AdsCommon::Errors::AuthError, 'Failed to authenticate.'
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
if page[:results]
|
84
|
-
# Increase query offset by page size.
|
85
|
-
offset += PAGE_SIZE
|
86
|
-
|
87
|
-
# Get the start index for printout.
|
88
|
-
start_index = page[:start_index]
|
89
|
-
|
90
|
-
# Print details about each user in results page.
|
91
|
-
page[:results].each_with_index do |user, index|
|
92
|
-
puts "%d) User ID: %d, name: %s, email: %s" %
|
93
|
-
[index + start_index, user[:id], user[:name], user[:email]]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end while offset < page[:total_result_set_size]
|
97
|
-
|
98
|
-
# Print a footer
|
99
|
-
if page.include?(:total_result_set_size)
|
100
|
-
puts "Total number of users: %d" % page[:total_result_set_size]
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Misc util to get the verification code from the console.
|
105
|
-
def get_verification_code(url)
|
106
|
-
puts "Hit Auth error, please navigate to URL:\n\t%s" % url
|
107
|
-
print 'Log in and type the verification code: '
|
108
|
-
verification_code = gets.chomp
|
109
|
-
return verification_code
|
110
|
-
end
|
111
|
-
|
112
|
-
if __FILE__ == $0
|
113
|
-
begin
|
114
|
-
oauth_handling()
|
115
|
-
|
116
|
-
# HTTP errors.
|
117
|
-
rescue AdsCommon::Errors::HttpError => e
|
118
|
-
puts "HTTP Error: %s" % e
|
119
|
-
|
120
|
-
# API errors.
|
121
|
-
rescue DfpApi::Errors::ApiException => e
|
122
|
-
puts "Message: %s" % e.message
|
123
|
-
puts 'Errors:'
|
124
|
-
e.errors.each_with_index do |error, index|
|
125
|
-
puts "\tError [%d]:" % (index + 1)
|
126
|
-
error.each do |field, value|
|
127
|
-
puts "\t\t%s: %s" % [field, value]
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
@@ -1,131 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# Encoding: utf-8
|
3
|
-
#
|
4
|
-
# Author:: api.dklimkin@gmail.com (Danial Klimkin)
|
5
|
-
#
|
6
|
-
# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
|
7
|
-
#
|
8
|
-
# License:: Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
-
# you may not use this file except in compliance with the License.
|
10
|
-
# You may obtain a copy of the License at
|
11
|
-
#
|
12
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
13
|
-
#
|
14
|
-
# Unless required by applicable law or agreed to in writing, software
|
15
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
17
|
-
# implied.
|
18
|
-
# See the License for the specific language governing permissions and
|
19
|
-
# limitations under the License.
|
20
|
-
#
|
21
|
-
# This example shows how to use OAuth1.0a authorization method. It is designed
|
22
|
-
# to be run from console and requires user input.
|
23
|
-
#
|
24
|
-
# NOTE: OAuth1.0a authorization method is deprecated, use OAuth2.0 instead.
|
25
|
-
# See oauth2_handling.rb for an example.
|
26
|
-
#
|
27
|
-
# Tags: UserService.getUsersByStatement
|
28
|
-
|
29
|
-
require 'dfp_api'
|
30
|
-
|
31
|
-
API_VERSION = :v201206
|
32
|
-
PAGE_SIZE = 500
|
33
|
-
MAX_RETRIES = 3
|
34
|
-
|
35
|
-
def oauth_handling()
|
36
|
-
# Get DfpApi instance and load configuration from ~/dfp_api.yml.
|
37
|
-
dfp = DfpApi::Api.new
|
38
|
-
|
39
|
-
# To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
|
40
|
-
# the configuration file or provide your own logger:
|
41
|
-
# dfp.logger = Logger.new('dfp_xml.log')
|
42
|
-
|
43
|
-
# Forcing authorization. A callback URL and other parameters could be
|
44
|
-
# specified as parameter for OAuth method.
|
45
|
-
token = dfp.authorize({:oauth_callback => 'oob'}) do |oauth_url|
|
46
|
-
# For command-line we ask user to go to URL and type in code.
|
47
|
-
verification_code = get_verification_code(oauth_url)
|
48
|
-
# Return verification code from the block.
|
49
|
-
(verification_code.empty?) ? nil : verification_code
|
50
|
-
end
|
51
|
-
|
52
|
-
# Get the UserService.
|
53
|
-
user_service = dfp.service(:UserService, API_VERSION)
|
54
|
-
|
55
|
-
# Define initial values.
|
56
|
-
offset = 0
|
57
|
-
page = Hash.new
|
58
|
-
retry_count = 0
|
59
|
-
|
60
|
-
begin
|
61
|
-
# Create statement for one page with current offset.
|
62
|
-
statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
|
63
|
-
|
64
|
-
begin
|
65
|
-
# Get users by statement.
|
66
|
-
page = user_service.get_users_by_statement(statement)
|
67
|
-
|
68
|
-
# The second way to do OAuth authentication is to make a request and catch
|
69
|
-
# the OAuthVerificationRequired exception. Add the verification code to the
|
70
|
-
# credentials once acquired.
|
71
|
-
rescue AdsCommon::Errors::OAuthVerificationRequired => e
|
72
|
-
if retry_count < MAX_RETRIES
|
73
|
-
verification_code = get_verification_code(e.oauth_url)
|
74
|
-
dfp.credential_handler.set_credential(
|
75
|
-
:oauth_verification_code, verification_code)
|
76
|
-
retry_count += 1
|
77
|
-
retry
|
78
|
-
else
|
79
|
-
raise AdsCommon::Errors::AuthError, 'Failed to authenticate.'
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
if page[:results]
|
84
|
-
# Increase query offset by page size.
|
85
|
-
offset += PAGE_SIZE
|
86
|
-
|
87
|
-
# Get the start index for printout.
|
88
|
-
start_index = page[:start_index]
|
89
|
-
|
90
|
-
# Print details about each user in results page.
|
91
|
-
page[:results].each_with_index do |user, index|
|
92
|
-
puts "%d) User ID: %d, name: %s, email: %s" %
|
93
|
-
[index + start_index, user[:id], user[:name], user[:email]]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end while offset < page[:total_result_set_size]
|
97
|
-
|
98
|
-
# Print a footer
|
99
|
-
if page.include?(:total_result_set_size)
|
100
|
-
puts "Total number of users: %d" % page[:total_result_set_size]
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Misc util to get the verification code from the console.
|
105
|
-
def get_verification_code(url)
|
106
|
-
puts "Hit Auth error, please navigate to URL:\n\t%s" % url
|
107
|
-
print 'Log in and type the verification code: '
|
108
|
-
verification_code = gets.chomp
|
109
|
-
return verification_code
|
110
|
-
end
|
111
|
-
|
112
|
-
if __FILE__ == $0
|
113
|
-
begin
|
114
|
-
oauth_handling()
|
115
|
-
|
116
|
-
# HTTP errors.
|
117
|
-
rescue AdsCommon::Errors::HttpError => e
|
118
|
-
puts "HTTP Error: %s" % e
|
119
|
-
|
120
|
-
# API errors.
|
121
|
-
rescue DfpApi::Errors::ApiException => e
|
122
|
-
puts "Message: %s" % e.message
|
123
|
-
puts 'Errors:'
|
124
|
-
e.errors.each_with_index do |error, index|
|
125
|
-
puts "\tError [%d]:" % (index + 1)
|
126
|
-
error.each do |field, value|
|
127
|
-
puts "\t\t%s: %s" % [field, value]
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
@@ -1,131 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# Encoding: utf-8
|
3
|
-
#
|
4
|
-
# Author:: api.dklimkin@gmail.com (Danial Klimkin)
|
5
|
-
#
|
6
|
-
# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
|
7
|
-
#
|
8
|
-
# License:: Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
-
# you may not use this file except in compliance with the License.
|
10
|
-
# You may obtain a copy of the License at
|
11
|
-
#
|
12
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
13
|
-
#
|
14
|
-
# Unless required by applicable law or agreed to in writing, software
|
15
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
17
|
-
# implied.
|
18
|
-
# See the License for the specific language governing permissions and
|
19
|
-
# limitations under the License.
|
20
|
-
#
|
21
|
-
# This example shows how to use OAuth1.0a authorization method. It is designed
|
22
|
-
# to be run from console and requires user input.
|
23
|
-
#
|
24
|
-
# NOTE: OAuth1.0a authorization method is deprecated, use OAuth2.0 instead.
|
25
|
-
# See oauth2_handling.rb for an example.
|
26
|
-
#
|
27
|
-
# Tags: UserService.getUsersByStatement
|
28
|
-
|
29
|
-
require 'dfp_api'
|
30
|
-
|
31
|
-
API_VERSION = :v201208
|
32
|
-
PAGE_SIZE = 500
|
33
|
-
MAX_RETRIES = 3
|
34
|
-
|
35
|
-
def oauth_handling()
|
36
|
-
# Get DfpApi instance and load configuration from ~/dfp_api.yml.
|
37
|
-
dfp = DfpApi::Api.new
|
38
|
-
|
39
|
-
# To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
|
40
|
-
# the configuration file or provide your own logger:
|
41
|
-
# dfp.logger = Logger.new('dfp_xml.log')
|
42
|
-
|
43
|
-
# Forcing authorization. A callback URL and other parameters could be
|
44
|
-
# specified as parameter for OAuth method.
|
45
|
-
token = dfp.authorize({:oauth_callback => 'oob'}) do |oauth_url|
|
46
|
-
# For command-line we ask user to go to URL and type in code.
|
47
|
-
verification_code = get_verification_code(oauth_url)
|
48
|
-
# Return verification code from the block.
|
49
|
-
(verification_code.empty?) ? nil : verification_code
|
50
|
-
end
|
51
|
-
|
52
|
-
# Get the UserService.
|
53
|
-
user_service = dfp.service(:UserService, API_VERSION)
|
54
|
-
|
55
|
-
# Define initial values.
|
56
|
-
offset = 0
|
57
|
-
page = Hash.new
|
58
|
-
retry_count = 0
|
59
|
-
|
60
|
-
begin
|
61
|
-
# Create statement for one page with current offset.
|
62
|
-
statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
|
63
|
-
|
64
|
-
begin
|
65
|
-
# Get users by statement.
|
66
|
-
page = user_service.get_users_by_statement(statement)
|
67
|
-
|
68
|
-
# The second way to do OAuth authentication is to make a request and catch
|
69
|
-
# the OAuthVerificationRequired exception. Add the verification code to the
|
70
|
-
# credentials once acquired.
|
71
|
-
rescue AdsCommon::Errors::OAuthVerificationRequired => e
|
72
|
-
if retry_count < MAX_RETRIES
|
73
|
-
verification_code = get_verification_code(e.oauth_url)
|
74
|
-
dfp.credential_handler.set_credential(
|
75
|
-
:oauth_verification_code, verification_code)
|
76
|
-
retry_count += 1
|
77
|
-
retry
|
78
|
-
else
|
79
|
-
raise AdsCommon::Errors::AuthError, 'Failed to authenticate.'
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
if page[:results]
|
84
|
-
# Increase query offset by page size.
|
85
|
-
offset += PAGE_SIZE
|
86
|
-
|
87
|
-
# Get the start index for printout.
|
88
|
-
start_index = page[:start_index]
|
89
|
-
|
90
|
-
# Print details about each user in results page.
|
91
|
-
page[:results].each_with_index do |user, index|
|
92
|
-
puts "%d) User ID: %d, name: %s, email: %s" %
|
93
|
-
[index + start_index, user[:id], user[:name], user[:email]]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end while offset < page[:total_result_set_size]
|
97
|
-
|
98
|
-
# Print a footer
|
99
|
-
if page.include?(:total_result_set_size)
|
100
|
-
puts "Total number of users: %d" % page[:total_result_set_size]
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Misc util to get the verification code from the console.
|
105
|
-
def get_verification_code(url)
|
106
|
-
puts "Hit Auth error, please navigate to URL:\n\t%s" % url
|
107
|
-
print 'Log in and type the verification code: '
|
108
|
-
verification_code = gets.chomp
|
109
|
-
return verification_code
|
110
|
-
end
|
111
|
-
|
112
|
-
if __FILE__ == $0
|
113
|
-
begin
|
114
|
-
oauth_handling()
|
115
|
-
|
116
|
-
# HTTP errors.
|
117
|
-
rescue AdsCommon::Errors::HttpError => e
|
118
|
-
puts "HTTP Error: %s" % e
|
119
|
-
|
120
|
-
# API errors.
|
121
|
-
rescue DfpApi::Errors::ApiException => e
|
122
|
-
puts "Message: %s" % e.message
|
123
|
-
puts 'Errors:'
|
124
|
-
e.errors.each_with_index do |error, index|
|
125
|
-
puts "\tError [%d]:" % (index + 1)
|
126
|
-
error.each do |field, value|
|
127
|
-
puts "\t\t%s: %s" % [field, value]
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
@@ -1,131 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# Encoding: utf-8
|
3
|
-
#
|
4
|
-
# Author:: api.dklimkin@gmail.com (Danial Klimkin)
|
5
|
-
#
|
6
|
-
# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
|
7
|
-
#
|
8
|
-
# License:: Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
-
# you may not use this file except in compliance with the License.
|
10
|
-
# You may obtain a copy of the License at
|
11
|
-
#
|
12
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
13
|
-
#
|
14
|
-
# Unless required by applicable law or agreed to in writing, software
|
15
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
17
|
-
# implied.
|
18
|
-
# See the License for the specific language governing permissions and
|
19
|
-
# limitations under the License.
|
20
|
-
#
|
21
|
-
# This example shows how to use OAuth1.0a authorization method. It is designed
|
22
|
-
# to be run from console and requires user input.
|
23
|
-
#
|
24
|
-
# NOTE: OAuth1.0a authorization method is deprecated, use OAuth2.0 instead.
|
25
|
-
# See oauth2_handling.rb for an example.
|
26
|
-
#
|
27
|
-
# Tags: UserService.getUsersByStatement
|
28
|
-
|
29
|
-
require 'dfp_api'
|
30
|
-
|
31
|
-
API_VERSION = :v201211
|
32
|
-
PAGE_SIZE = 500
|
33
|
-
MAX_RETRIES = 3
|
34
|
-
|
35
|
-
def oauth_handling()
|
36
|
-
# Get DfpApi instance and load configuration from ~/dfp_api.yml.
|
37
|
-
dfp = DfpApi::Api.new
|
38
|
-
|
39
|
-
# To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
|
40
|
-
# the configuration file or provide your own logger:
|
41
|
-
# dfp.logger = Logger.new('dfp_xml.log')
|
42
|
-
|
43
|
-
# Forcing authorization. A callback URL and other parameters could be
|
44
|
-
# specified as parameter for OAuth method.
|
45
|
-
token = dfp.authorize({:oauth_callback => 'oob'}) do |oauth_url|
|
46
|
-
# For command-line we ask user to go to URL and type in code.
|
47
|
-
verification_code = get_verification_code(oauth_url)
|
48
|
-
# Return verification code from the block.
|
49
|
-
(verification_code.empty?) ? nil : verification_code
|
50
|
-
end
|
51
|
-
|
52
|
-
# Get the UserService.
|
53
|
-
user_service = dfp.service(:UserService, API_VERSION)
|
54
|
-
|
55
|
-
# Define initial values.
|
56
|
-
offset = 0
|
57
|
-
page = Hash.new
|
58
|
-
retry_count = 0
|
59
|
-
|
60
|
-
begin
|
61
|
-
# Create statement for one page with current offset.
|
62
|
-
statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
|
63
|
-
|
64
|
-
begin
|
65
|
-
# Get users by statement.
|
66
|
-
page = user_service.get_users_by_statement(statement)
|
67
|
-
|
68
|
-
# The second way to do OAuth authentication is to make a request and catch
|
69
|
-
# the OAuthVerificationRequired exception. Add the verification code to the
|
70
|
-
# credentials once acquired.
|
71
|
-
rescue AdsCommon::Errors::OAuthVerificationRequired => e
|
72
|
-
if retry_count < MAX_RETRIES
|
73
|
-
verification_code = get_verification_code(e.oauth_url)
|
74
|
-
dfp.credential_handler.set_credential(
|
75
|
-
:oauth_verification_code, verification_code)
|
76
|
-
retry_count += 1
|
77
|
-
retry
|
78
|
-
else
|
79
|
-
raise AdsCommon::Errors::AuthError, 'Failed to authenticate.'
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
if page[:results]
|
84
|
-
# Increase query offset by page size.
|
85
|
-
offset += PAGE_SIZE
|
86
|
-
|
87
|
-
# Get the start index for printout.
|
88
|
-
start_index = page[:start_index]
|
89
|
-
|
90
|
-
# Print details about each user in results page.
|
91
|
-
page[:results].each_with_index do |user, index|
|
92
|
-
puts "%d) User ID: %d, name: %s, email: %s" %
|
93
|
-
[index + start_index, user[:id], user[:name], user[:email]]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end while offset < page[:total_result_set_size]
|
97
|
-
|
98
|
-
# Print a footer
|
99
|
-
if page.include?(:total_result_set_size)
|
100
|
-
puts "Total number of users: %d" % page[:total_result_set_size]
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Misc util to get the verification code from the console.
|
105
|
-
def get_verification_code(url)
|
106
|
-
puts "Hit Auth error, please navigate to URL:\n\t%s" % url
|
107
|
-
print 'Log in and type the verification code: '
|
108
|
-
verification_code = gets.chomp
|
109
|
-
return verification_code
|
110
|
-
end
|
111
|
-
|
112
|
-
if __FILE__ == $0
|
113
|
-
begin
|
114
|
-
oauth_handling()
|
115
|
-
|
116
|
-
# HTTP errors.
|
117
|
-
rescue AdsCommon::Errors::HttpError => e
|
118
|
-
puts "HTTP Error: %s" % e
|
119
|
-
|
120
|
-
# API errors.
|
121
|
-
rescue DfpApi::Errors::ApiException => e
|
122
|
-
puts "Message: %s" % e.message
|
123
|
-
puts 'Errors:'
|
124
|
-
e.errors.each_with_index do |error, index|
|
125
|
-
puts "\tError [%d]:" % (index + 1)
|
126
|
-
error.each do |field, value|
|
127
|
-
puts "\t\t%s: %s" % [field, value]
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|