google-dfp-api 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +4 -0
- data/README +17 -2
- data/Rakefile +3 -5
- data/examples/v201101/common/oauth_handling.rb +128 -0
- data/examples/v201101/company_service/create_companies.rb +1 -1
- data/examples/v201101/company_service/get_all_companies.rb +1 -1
- data/examples/v201101/creative_service/create_creatives.rb +1 -1
- data/examples/v201101/creative_service/get_all_creatives.rb +1 -1
- data/examples/v201101/custom_targeting_service/delete_custom_targeting_keys.rb +2 -2
- data/examples/v201101/custom_targeting_service/delete_custom_targeting_values.rb +2 -2
- data/examples/v201101/custom_targeting_service/get_all_custom_targeting_keys_and_values.rb +2 -2
- data/examples/v201101/inventory_service/create_ad_units.rb +1 -1
- data/examples/v201101/inventory_service/deactivate_ad_units.rb +2 -2
- data/examples/v201101/inventory_service/get_ad_units_by_statement.rb +1 -1
- data/examples/v201101/inventory_service/get_all_ad_units.rb +1 -1
- data/examples/v201101/line_item_creative_association_service/deactivate_licas.rb +2 -2
- data/examples/v201101/line_item_creative_association_service/get_all_licas.rb +1 -1
- data/examples/v201101/line_item_service/activate_line_items.rb +2 -2
- data/examples/v201101/line_item_service/create_line_items.rb +2 -2
- data/examples/v201101/line_item_service/get_all_line_items.rb +1 -1
- data/examples/v201101/order_service/approve_orders.rb +2 -2
- data/examples/v201101/order_service/create_orders.rb +1 -1
- data/examples/v201101/order_service/get_all_orders.rb +1 -1
- data/examples/v201101/placement_service/create_placements.rb +3 -3
- data/examples/v201101/placement_service/deactivate_placements.rb +2 -2
- data/examples/v201101/placement_service/get_all_placements.rb +1 -1
- data/examples/v201101/user_service/get_all_users.rb +1 -1
- data/examples/v201103/common/oauth_handling.rb +128 -0
- data/examples/v201103/company_service/create_companies.rb +1 -1
- data/examples/v201103/company_service/get_all_companies.rb +1 -1
- data/examples/v201103/creative_service/create_creatives.rb +1 -1
- data/examples/v201103/creative_service/get_all_creatives.rb +1 -1
- data/examples/v201103/custom_targeting_service/delete_custom_targeting_keys.rb +2 -2
- data/examples/v201103/custom_targeting_service/delete_custom_targeting_values.rb +2 -2
- data/examples/v201103/custom_targeting_service/get_all_custom_targeting_keys_and_values.rb +2 -2
- data/examples/v201103/inventory_service/create_ad_units.rb +1 -1
- data/examples/v201103/inventory_service/deactivate_ad_units.rb +2 -2
- data/examples/v201103/inventory_service/get_ad_units_by_statement.rb +1 -1
- data/examples/v201103/inventory_service/get_all_ad_units.rb +1 -1
- data/examples/v201103/line_item_creative_association_service/deactivate_licas.rb +2 -2
- data/examples/v201103/line_item_creative_association_service/get_all_licas.rb +1 -1
- data/examples/v201103/line_item_service/activate_line_items.rb +2 -2
- data/examples/v201103/line_item_service/create_line_items.rb +2 -2
- data/examples/v201103/line_item_service/get_all_line_items.rb +1 -1
- data/examples/v201103/order_service/approve_orders.rb +2 -2
- data/examples/v201103/order_service/create_orders.rb +1 -1
- data/examples/v201103/order_service/get_all_orders.rb +1 -1
- data/examples/v201103/placement_service/create_placements.rb +3 -3
- data/examples/v201103/placement_service/deactivate_placements.rb +2 -2
- data/examples/v201103/placement_service/get_all_placements.rb +1 -1
- data/examples/v201103/user_service/get_all_users.rb +1 -1
- data/examples/v201104/common/oauth_handling.rb +128 -0
- data/examples/v201104/company_service/create_companies.rb +1 -1
- data/examples/v201104/company_service/get_all_companies.rb +1 -1
- data/examples/v201104/creative_service/create_creatives.rb +1 -1
- data/examples/v201104/creative_service/get_all_creatives.rb +1 -1
- data/examples/v201104/custom_targeting_service/delete_custom_targeting_keys.rb +2 -2
- data/examples/v201104/custom_targeting_service/delete_custom_targeting_values.rb +2 -2
- data/examples/v201104/custom_targeting_service/get_all_custom_targeting_keys_and_values.rb +2 -2
- data/examples/v201104/inventory_service/create_ad_units.rb +1 -1
- data/examples/v201104/inventory_service/deactivate_ad_units.rb +2 -2
- data/examples/v201104/inventory_service/get_ad_units_by_statement.rb +1 -1
- data/examples/v201104/inventory_service/get_all_ad_units.rb +1 -1
- data/examples/v201104/line_item_creative_association_service/deactivate_licas.rb +2 -2
- data/examples/v201104/line_item_creative_association_service/get_all_licas.rb +1 -1
- data/examples/v201104/line_item_service/activate_line_items.rb +2 -2
- data/examples/v201104/line_item_service/create_line_items.rb +1 -1
- data/examples/v201104/line_item_service/get_all_line_items.rb +1 -1
- data/examples/v201104/order_service/approve_orders.rb +2 -2
- data/examples/v201104/order_service/create_orders.rb +1 -1
- data/examples/v201104/order_service/get_all_orders.rb +1 -1
- data/examples/v201104/placement_service/create_placements.rb +3 -3
- data/examples/v201104/placement_service/deactivate_placements.rb +2 -2
- data/examples/v201104/placement_service/get_all_placements.rb +1 -1
- data/examples/v201104/user_service/get_all_users.rb +1 -1
- data/lib/dfp_api.rb +24 -70
- data/lib/dfp_api/api_config.rb +3 -1
- data/lib/dfp_api/client_login_header_handler.rb +56 -0
- data/lib/dfp_api/credential_handler.rb +19 -14
- data/lib/dfp_api/errors.rb +1 -1
- data/lib/dfp_api/v201101/company_service.rb +3 -3
- data/lib/dfp_api/v201101/company_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/creative_service.rb +3 -3
- data/lib/dfp_api/v201101/creative_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/custom_targeting_service.rb +7 -7
- data/lib/dfp_api/v201101/custom_targeting_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/forecast_service.rb +3 -3
- data/lib/dfp_api/v201101/forecast_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/inventory_service.rb +7 -7
- data/lib/dfp_api/v201101/inventory_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/line_item_creative_association_service.rb +7 -7
- data/lib/dfp_api/v201101/line_item_creative_association_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/line_item_service.rb +7 -7
- data/lib/dfp_api/v201101/line_item_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/network_service.rb +7 -7
- data/lib/dfp_api/v201101/network_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/order_service.rb +11 -11
- data/lib/dfp_api/v201101/order_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/placement_service.rb +3 -3
- data/lib/dfp_api/v201101/placement_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/publisher_query_language_service.rb +3 -3
- data/lib/dfp_api/v201101/publisher_query_language_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/report_service.rb +3 -3
- data/lib/dfp_api/v201101/report_service_registry.rb +9 -4
- data/lib/dfp_api/v201101/user_service.rb +7 -7
- data/lib/dfp_api/v201101/user_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/company_service.rb +3 -3
- data/lib/dfp_api/v201103/company_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/creative_service.rb +3 -3
- data/lib/dfp_api/v201103/creative_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/custom_targeting_service.rb +7 -7
- data/lib/dfp_api/v201103/custom_targeting_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/forecast_service.rb +3 -3
- data/lib/dfp_api/v201103/forecast_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/inventory_service.rb +7 -7
- data/lib/dfp_api/v201103/inventory_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/line_item_creative_association_service.rb +7 -7
- data/lib/dfp_api/v201103/line_item_creative_association_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/line_item_service.rb +7 -7
- data/lib/dfp_api/v201103/line_item_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/network_service.rb +7 -7
- data/lib/dfp_api/v201103/network_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/order_service.rb +11 -11
- data/lib/dfp_api/v201103/order_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/placement_service.rb +3 -3
- data/lib/dfp_api/v201103/placement_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/publisher_query_language_service.rb +3 -3
- data/lib/dfp_api/v201103/publisher_query_language_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/report_service.rb +3 -3
- data/lib/dfp_api/v201103/report_service_registry.rb +9 -4
- data/lib/dfp_api/v201103/user_service.rb +7 -7
- data/lib/dfp_api/v201103/user_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/company_service.rb +3 -3
- data/lib/dfp_api/v201104/company_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/creative_service.rb +3 -3
- data/lib/dfp_api/v201104/creative_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/custom_targeting_service.rb +7 -7
- data/lib/dfp_api/v201104/custom_targeting_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/forecast_service.rb +3 -3
- data/lib/dfp_api/v201104/forecast_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/inventory_service.rb +7 -7
- data/lib/dfp_api/v201104/inventory_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/line_item_creative_association_service.rb +7 -7
- data/lib/dfp_api/v201104/line_item_creative_association_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/line_item_service.rb +7 -7
- data/lib/dfp_api/v201104/line_item_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/network_service.rb +7 -7
- data/lib/dfp_api/v201104/network_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/order_service.rb +11 -11
- data/lib/dfp_api/v201104/order_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/placement_service.rb +3 -3
- data/lib/dfp_api/v201104/placement_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/publisher_query_language_service.rb +3 -3
- data/lib/dfp_api/v201104/publisher_query_language_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/report_service.rb +3 -3
- data/lib/dfp_api/v201104/report_service_registry.rb +9 -4
- data/lib/dfp_api/v201104/user_service.rb +7 -7
- data/lib/dfp_api/v201104/user_service_registry.rb +9 -4
- data/test/bugs/test_issue_00000016.rb +6 -5
- data/test/dfp_api/test_dfp_api.rb +29 -7
- metadata +257 -253
@@ -0,0 +1,128 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
#
|
3
|
+
# Author:: api.dklimkin@gmail.com (Danial Klimkin)
|
4
|
+
#
|
5
|
+
# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# License:: Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied.
|
17
|
+
# See the License for the specific language governing permissions and
|
18
|
+
# limitations under the License.
|
19
|
+
#
|
20
|
+
# This example shows how to use OAuth authorization method. It is designed to be
|
21
|
+
# run from console and requires user input.
|
22
|
+
#
|
23
|
+
# Tags: UserService.getUsersByStatement
|
24
|
+
|
25
|
+
require 'rubygems'
|
26
|
+
require 'dfp_api'
|
27
|
+
|
28
|
+
API_VERSION = :v201103
|
29
|
+
PAGE_SIZE = 500
|
30
|
+
MAX_RETRIES = 3
|
31
|
+
|
32
|
+
def oauth_handling()
|
33
|
+
# Get DfpApi instance and load configuration from ~/dfp_api.yml.
|
34
|
+
dfp = DfpApi::Api.new
|
35
|
+
|
36
|
+
# To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
|
37
|
+
# the configuration file or provide your own logger:
|
38
|
+
# dfp.logger = Logger.new('dfp_xml.log')
|
39
|
+
|
40
|
+
# Forcing authorization. A callback URL and other parameters could be
|
41
|
+
# specified as parameter for OAuth method.
|
42
|
+
token = dfp.authorize({:oauth_callback => 'oob'}) do |oauth_url|
|
43
|
+
# For command-line we ask user to go to URL and type in code.
|
44
|
+
verification_code = get_verification_code(oauth_url)
|
45
|
+
# Return verification code from the block.
|
46
|
+
(verification_code.empty?) ? nil : verification_code
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get the UserService.
|
50
|
+
user_service = dfp.service(:UserService, API_VERSION)
|
51
|
+
|
52
|
+
# Define initial values.
|
53
|
+
offset = 0
|
54
|
+
page = Hash.new
|
55
|
+
retry_count = 0
|
56
|
+
|
57
|
+
begin
|
58
|
+
# Create statement for one page with current offset.
|
59
|
+
statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
|
60
|
+
|
61
|
+
begin
|
62
|
+
# Get users by statement.
|
63
|
+
page = user_service.get_users_by_statement(statement)
|
64
|
+
|
65
|
+
# The second way to do OAuth authentication is to make a request and catch
|
66
|
+
# the OAuthVerificationRequired exception. Add the verification code to the
|
67
|
+
# credentials once acquired.
|
68
|
+
rescue AdsCommon::Errors::OAuthVerificationRequired => e
|
69
|
+
if retry_count < MAX_RETRIES
|
70
|
+
verification_code = get_verification_code(e.oauth_url)
|
71
|
+
dfp.credential_handler.set_credential(
|
72
|
+
:oauth_verification_code, verification_code)
|
73
|
+
retry_count += 1
|
74
|
+
retry
|
75
|
+
else
|
76
|
+
raise AdsCommon::Errors::AuthError, 'Failed to authenticate.'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
if page[:results]
|
81
|
+
# Increase query offset by page size.
|
82
|
+
offset += PAGE_SIZE
|
83
|
+
|
84
|
+
# Get the start index for printout.
|
85
|
+
start_index = page[:start_index]
|
86
|
+
|
87
|
+
# Print details about each user in results page.
|
88
|
+
page[:results].each_with_index do |user, index|
|
89
|
+
puts "%d) User ID: %d, name: %s, email: %s" %
|
90
|
+
[index + start_index, user[:id], user[:name], user[:email]]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end while offset < page[:total_result_set_size]
|
94
|
+
|
95
|
+
# Print a footer
|
96
|
+
if page.include?(:total_result_set_size)
|
97
|
+
puts "Total number of users: %d" % page[:total_result_set_size]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Misc util to get the verification code from the console.
|
102
|
+
def get_verification_code(url)
|
103
|
+
puts "Hit Auth error, please navigate to URL:\n\t%s" % url
|
104
|
+
print 'Log in and type the verification code: '
|
105
|
+
verification_code = gets.chomp
|
106
|
+
return verification_code
|
107
|
+
end
|
108
|
+
|
109
|
+
if __FILE__ == $0
|
110
|
+
begin
|
111
|
+
oauth_handling()
|
112
|
+
|
113
|
+
# HTTP errors.
|
114
|
+
rescue AdsCommon::Errors::HttpError => e
|
115
|
+
puts "HTTP Error: %s" % e
|
116
|
+
|
117
|
+
# API errors.
|
118
|
+
rescue DfpApi::Errors::ApiException => e
|
119
|
+
puts "Message: %s" % e.message
|
120
|
+
puts 'Errors:'
|
121
|
+
e.errors.each_with_index do |error, index|
|
122
|
+
puts "\tError [%d]:" % (index + 1)
|
123
|
+
error.each do |field, value|
|
124
|
+
puts "\t\t%s: %s" % [field, value]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -41,7 +41,7 @@ def create_companies()
|
|
41
41
|
company_service = dfp.service(:CompanyService, API_VERSION)
|
42
42
|
|
43
43
|
# Create an array to store local company objects.
|
44
|
-
companies =
|
44
|
+
companies = []
|
45
45
|
ITEM_COUNT.times do |index|
|
46
46
|
companies << {:name => "Advertiser #%d" % index, :type => 'ADVERTISER'}
|
47
47
|
end
|
@@ -42,7 +42,7 @@ def get_all_custom_targeting_keys_and_values()
|
|
42
42
|
|
43
43
|
# Define initial values.
|
44
44
|
offset = 0
|
45
|
-
page =
|
45
|
+
page = {}
|
46
46
|
|
47
47
|
begin
|
48
48
|
# Create a statement to get one page with current offset.
|
@@ -82,7 +82,7 @@ end
|
|
82
82
|
def print_all_values_for_key(custom_targeting_service, custom_targeting_key)
|
83
83
|
# Define initial values.
|
84
84
|
offset = 0
|
85
|
-
page =
|
85
|
+
page = {}
|
86
86
|
|
87
87
|
# Create a statement to get values for given key.
|
88
88
|
statement_text = "WHERE customTargetingKeyId = :key_id LIMIT %d" % PAGE_SIZE
|
@@ -51,7 +51,7 @@ def create_ad_units()
|
|
51
51
|
puts "Using effective root ad unit: %d" % effective_root_ad_unit_id
|
52
52
|
|
53
53
|
# Create an array to store local ad unit objects.
|
54
|
-
ad_units =
|
54
|
+
ad_units = []
|
55
55
|
ITEM_COUNT.times do |index|
|
56
56
|
ad_units << {
|
57
57
|
:name => "Ad_Unit_%d" % index,
|
@@ -61,7 +61,7 @@ def get_ad_units_by_statement()
|
|
61
61
|
page = inventory_service.get_ad_units_by_statement(statement)
|
62
62
|
|
63
63
|
if page[:results]
|
64
|
-
# Print details about each
|
64
|
+
# Print details about each ad unit in results.
|
65
65
|
page[:results].each_with_index do |ad_unit, index|
|
66
66
|
puts "%d) Ad unit ID: %d, name: %s, status: %s." %
|
67
67
|
[index, ad_unit[:id], ad_unit[:name], ad_unit[:status]]
|
@@ -53,7 +53,7 @@ def create_line_items()
|
|
53
53
|
inventory_targeting = {:targeted_placement_ids => targeted_placement_ids}
|
54
54
|
|
55
55
|
# Create geographical targeting.
|
56
|
-
geo_targeting =
|
56
|
+
geo_targeting = {}
|
57
57
|
# Include the US and Quebec, Canada.
|
58
58
|
geo_targeting[:targeted_locations] = [
|
59
59
|
{:xsi_type => 'CountryLocation', :country_code => 'US'},
|
@@ -96,7 +96,7 @@ def create_line_items()
|
|
96
96
|
}
|
97
97
|
|
98
98
|
# Create an array to store local line item objects.
|
99
|
-
line_items =
|
99
|
+
line_items = []
|
100
100
|
ITEM_COUNT.times do |index|
|
101
101
|
line_item = {:name => "Line item #%d" % index,
|
102
102
|
:order_id => order_id,
|
@@ -49,7 +49,7 @@ def create_orders()
|
|
49
49
|
trafficker_id = 'INSERT_TRAFFICKER_ID_HERE'.to_i
|
50
50
|
|
51
51
|
# Create an array to store local order objects.
|
52
|
-
orders =
|
52
|
+
orders = []
|
53
53
|
ITEM_COUNT.times do |index|
|
54
54
|
orders << {:name => "Order #%d" % index,
|
55
55
|
:advertiser_id => advertiser_id,
|
@@ -46,21 +46,21 @@ def create_placements()
|
|
46
46
|
skyscraper_ad_unit_placement = {
|
47
47
|
:name => "Skyscraper AdUnit Placement #%d" % (Time.new.to_f * 1000),
|
48
48
|
:description => 'Contains ad units for creatives of size 120x600',
|
49
|
-
:targeted_ad_unit_ids =>
|
49
|
+
:targeted_ad_unit_ids => []
|
50
50
|
}
|
51
51
|
|
52
52
|
# Create local placement object to store medium square ad units.
|
53
53
|
medium_square_ad_unit_placement = {
|
54
54
|
:name => "Medium Square AdUnit Placement #%d" % (Time.new.to_f * 1000),
|
55
55
|
:description => 'Contains ad units for creatives of size 300x250',
|
56
|
-
:targeted_ad_unit_ids =>
|
56
|
+
:targeted_ad_unit_ids => []
|
57
57
|
}
|
58
58
|
|
59
59
|
# Create local placement object to store banner ad units.
|
60
60
|
banner_ad_unit_placement = {
|
61
61
|
:name => "Banner AdUnit Placement #%d" % (Time.new.to_f * 1000),
|
62
62
|
:description => 'Contains ad units for creatives of size 468x60',
|
63
|
-
:targeted_ad_unit_ids =>
|
63
|
+
:targeted_ad_unit_ids => []
|
64
64
|
}
|
65
65
|
|
66
66
|
# Get the first 500 ad units.
|
@@ -0,0 +1,128 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
#
|
3
|
+
# Author:: api.dklimkin@gmail.com (Danial Klimkin)
|
4
|
+
#
|
5
|
+
# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# License:: Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied.
|
17
|
+
# See the License for the specific language governing permissions and
|
18
|
+
# limitations under the License.
|
19
|
+
#
|
20
|
+
# This example shows how to use OAuth authorization method. It is designed to be
|
21
|
+
# run from console and requires user input.
|
22
|
+
#
|
23
|
+
# Tags: UserService.getUsersByStatement
|
24
|
+
|
25
|
+
require 'rubygems'
|
26
|
+
require 'dfp_api'
|
27
|
+
|
28
|
+
API_VERSION = :v201104
|
29
|
+
PAGE_SIZE = 500
|
30
|
+
MAX_RETRIES = 3
|
31
|
+
|
32
|
+
def oauth_handling()
|
33
|
+
# Get DfpApi instance and load configuration from ~/dfp_api.yml.
|
34
|
+
dfp = DfpApi::Api.new
|
35
|
+
|
36
|
+
# To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
|
37
|
+
# the configuration file or provide your own logger:
|
38
|
+
# dfp.logger = Logger.new('dfp_xml.log')
|
39
|
+
|
40
|
+
# Forcing authorization. A callback URL and other parameters could be
|
41
|
+
# specified as parameter for OAuth method.
|
42
|
+
token = dfp.authorize({:oauth_callback => 'oob'}) do |oauth_url|
|
43
|
+
# For command-line we ask user to go to URL and type in code.
|
44
|
+
verification_code = get_verification_code(oauth_url)
|
45
|
+
# Return verification code from the block.
|
46
|
+
(verification_code.empty?) ? nil : verification_code
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get the UserService.
|
50
|
+
user_service = dfp.service(:UserService, API_VERSION)
|
51
|
+
|
52
|
+
# Define initial values.
|
53
|
+
offset = 0
|
54
|
+
page = Hash.new
|
55
|
+
retry_count = 0
|
56
|
+
|
57
|
+
begin
|
58
|
+
# Create statement for one page with current offset.
|
59
|
+
statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
|
60
|
+
|
61
|
+
begin
|
62
|
+
# Get users by statement.
|
63
|
+
page = user_service.get_users_by_statement(statement)
|
64
|
+
|
65
|
+
# The second way to do OAuth authentication is to make a request and catch
|
66
|
+
# the OAuthVerificationRequired exception. Add the verification code to the
|
67
|
+
# credentials once acquired.
|
68
|
+
rescue AdsCommon::Errors::OAuthVerificationRequired => e
|
69
|
+
if retry_count < MAX_RETRIES
|
70
|
+
verification_code = get_verification_code(e.oauth_url)
|
71
|
+
dfp.credential_handler.set_credential(
|
72
|
+
:oauth_verification_code, verification_code)
|
73
|
+
retry_count += 1
|
74
|
+
retry
|
75
|
+
else
|
76
|
+
raise AdsCommon::Errors::AuthError, 'Failed to authenticate.'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
if page[:results]
|
81
|
+
# Increase query offset by page size.
|
82
|
+
offset += PAGE_SIZE
|
83
|
+
|
84
|
+
# Get the start index for printout.
|
85
|
+
start_index = page[:start_index]
|
86
|
+
|
87
|
+
# Print details about each user in results page.
|
88
|
+
page[:results].each_with_index do |user, index|
|
89
|
+
puts "%d) User ID: %d, name: %s, email: %s" %
|
90
|
+
[index + start_index, user[:id], user[:name], user[:email]]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end while offset < page[:total_result_set_size]
|
94
|
+
|
95
|
+
# Print a footer
|
96
|
+
if page.include?(:total_result_set_size)
|
97
|
+
puts "Total number of users: %d" % page[:total_result_set_size]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Misc util to get the verification code from the console.
|
102
|
+
def get_verification_code(url)
|
103
|
+
puts "Hit Auth error, please navigate to URL:\n\t%s" % url
|
104
|
+
print 'Log in and type the verification code: '
|
105
|
+
verification_code = gets.chomp
|
106
|
+
return verification_code
|
107
|
+
end
|
108
|
+
|
109
|
+
if __FILE__ == $0
|
110
|
+
begin
|
111
|
+
oauth_handling()
|
112
|
+
|
113
|
+
# HTTP errors.
|
114
|
+
rescue AdsCommon::Errors::HttpError => e
|
115
|
+
puts "HTTP Error: %s" % e
|
116
|
+
|
117
|
+
# API errors.
|
118
|
+
rescue DfpApi::Errors::ApiException => e
|
119
|
+
puts "Message: %s" % e.message
|
120
|
+
puts 'Errors:'
|
121
|
+
e.errors.each_with_index do |error, index|
|
122
|
+
puts "\tError [%d]:" % (index + 1)
|
123
|
+
error.each do |field, value|
|
124
|
+
puts "\t\t%s: %s" % [field, value]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|