Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d50cd6

Browse files
Adding Ad Exchange Seller REST API samples.
1 parent 959d241 commit 4d50cd6

15 files changed

+915
-0
lines changed

samples/adexchangeseller/README

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A collection of command-line samples for the Ad Exchange Seller REST API.
2+
3+
api: adexchangeseller
4+
keywords: cmdline
5+
author: Sérgio Gomes ([email protected])
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"installed": {
3+
"client_id": "[[INSERT CLIENT ID HERE]]",
4+
"client_secret": "[[INSERT CLIENT SECRET HERE]]",
5+
"redirect_uris": [],
6+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
7+
"token_uri": "https://accounts.google.com/o/oauth2/token"
8+
}
9+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2013 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Retrieves a saved report, or a report for the specified ad client.
18+
19+
To get ad clients, run get_all_ad_clients.py.
20+
21+
Tags: reports.generate
22+
"""
23+
24+
__author__ = '[email protected] (Sérgio Gomes)'
25+
26+
import argparse
27+
import sys
28+
29+
from apiclient import sample_tools
30+
from oauth2client import client
31+
32+
# Declare command-line flags.
33+
argparser = argparse.ArgumentParser(add_help=False)
34+
argparser.add_argument(
35+
'--ad_client_id',
36+
help='The ID of the ad client for which to generate a report')
37+
argparser.add_argument(
38+
'--report_id',
39+
help='The ID of the saved report to generate')
40+
41+
42+
def main(argv):
43+
# Authenticate and construct service.
44+
service, flags = sample_tools.init(
45+
argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[argparser],
46+
scope='https://www.googleapis.com/auth/adexchange.seller.readonly')
47+
48+
# Process flags and read their values.
49+
ad_client_id = flags.ad_client_id
50+
saved_report_id = flags.report_id
51+
52+
try:
53+
# Retrieve report.
54+
if saved_report_id:
55+
result = service.reports().saved().generate(
56+
savedReportId=saved_report_id).execute()
57+
elif ad_client_id:
58+
result = service.reports().generate(
59+
startDate='2011-01-01', endDate='2011-08-31',
60+
filter=['AD_CLIENT_ID==' + ad_client_id],
61+
metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
62+
'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
63+
'AD_REQUESTS_RPM', 'EARNINGS'],
64+
dimension=['DATE'],
65+
sort=['+DATE']).execute()
66+
else:
67+
argparser.print_help()
68+
sys.exit(1)
69+
# Display headers.
70+
for header in result['headers']:
71+
print '%25s' % header['name'],
72+
print
73+
74+
# Display results.
75+
for row in result['rows']:
76+
for column in row:
77+
print '%25s' % column,
78+
print
79+
80+
except client.AccessTokenRefreshError:
81+
print ('The credentials have been revoked or expired, please re-run the '
82+
'application to re-authorize')
83+
84+
if __name__ == '__main__':
85+
main(sys.argv)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2013 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example retrieves a report for the specified ad client.
18+
19+
Please only use pagination if your application requires it due to memory or
20+
storage constraints.
21+
If you need to retrieve more than 5000 rows, please check generate_report.py, as
22+
due to current limitations you will not be able to use paging for large reports.
23+
To get ad clients, run get_all_ad_clients.py.
24+
25+
Tags: reports.generate
26+
"""
27+
28+
__author__ = '[email protected] (Sérgio Gomes)'
29+
30+
import argparse
31+
import sys
32+
33+
from apiclient import sample_tools
34+
from oauth2client import client
35+
36+
MAX_PAGE_SIZE = 50
37+
# This is the maximum number of obtainable rows for paged reports.
38+
ROW_LIMIT = 5000
39+
40+
# Declare command-line flags.
41+
argparser = argparse.ArgumentParser(add_help=False)
42+
argparser.add_argument('ad_client_id',
43+
help='The ID of the ad client for which to generate a report')
44+
45+
46+
def main(argv):
47+
# Authenticate and construct service.
48+
service, flags = sample_tools.init(
49+
argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[argparser],
50+
scope='https://www.googleapis.com/auth/adexchange.seller.readonly')
51+
52+
ad_client_id = flags.ad_client_id
53+
54+
try:
55+
# Retrieve report in pages and display data as we receive it.
56+
start_index = 0
57+
rows_to_obtain = MAX_PAGE_SIZE
58+
while True:
59+
result = service.reports().generate(
60+
startDate='2011-01-01', endDate='2011-08-31',
61+
filter=['AD_CLIENT_ID==' + ad_client_id],
62+
metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
63+
'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
64+
'AD_REQUESTS_RPM', 'EARNINGS'],
65+
dimension=['DATE'],
66+
sort=['+DATE'],
67+
startIndex=start_index,
68+
maxResults=rows_to_obtain).execute()
69+
70+
# If this is the first page, display the headers.
71+
if start_index == 0:
72+
for header in result['headers']:
73+
print '%25s' % header['name'],
74+
print
75+
76+
# Display results for this page.
77+
for row in result['rows']:
78+
for column in row:
79+
print '%25s' % column,
80+
print
81+
82+
start_index += len(result['rows'])
83+
84+
# Check to see if we're going to go above the limit and get as many
85+
# results as we can.
86+
if start_index + MAX_PAGE_SIZE > ROW_LIMIT:
87+
rows_to_obtain = ROW_LIMIT - start_index
88+
if rows_to_obtain <= 0:
89+
break
90+
91+
if (start_index >= int(result['totalMatchedRows'])):
92+
break
93+
94+
except client.AccessTokenRefreshError:
95+
print ('The credentials have been revoked or expired, please re-run the '
96+
'application to re-authorize')
97+
98+
if __name__ == '__main__':
99+
main(sys.argv)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2013 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example gets all ad clients for the logged in user's account.
18+
19+
Tags: adclients.list
20+
"""
21+
22+
__author__ = '[email protected] (Sérgio Gomes)'
23+
24+
import sys
25+
26+
from apiclient import sample_tools
27+
from oauth2client import client
28+
29+
MAX_PAGE_SIZE = 50
30+
31+
32+
def main(argv):
33+
# Authenticate and construct service.
34+
service, flags = sample_tools.init(
35+
argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[],
36+
scope='https://www.googleapis.com/auth/adexchange.seller.readonly')
37+
38+
try:
39+
# Retrieve ad client list in pages and display data as we receive it.
40+
request = service.adclients().list(maxResults=MAX_PAGE_SIZE)
41+
42+
while request is not None:
43+
result = request.execute()
44+
ad_clients = result['items']
45+
for ad_client in ad_clients:
46+
print ('Ad client for product "%s" with ID "%s" was found. '
47+
% (ad_client['productCode'], ad_client['id']))
48+
49+
print ('\tSupports reporting: %s' %
50+
(ad_client['supportsReporting'] and 'Yes' or 'No'))
51+
52+
request = service.adclients().list_next(request, result)
53+
54+
except client.AccessTokenRefreshError:
55+
print ('The credentials have been revoked or expired, please re-run the '
56+
'application to re-authorize')
57+
58+
if __name__ == '__main__':
59+
main(sys.argv)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2013 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example gets all ad units in an ad client.
18+
19+
To get ad clients, run get_all_ad_clients.py.
20+
21+
Tags: adunits.list
22+
"""
23+
24+
__author__ = '[email protected] (Sérgio Gomes)'
25+
26+
import argparse
27+
import sys
28+
29+
from apiclient import sample_tools
30+
from oauth2client import client
31+
32+
# Declare command-line flags.
33+
argparser = argparse.ArgumentParser(add_help=False)
34+
argparser.add_argument('ad_client_id',
35+
help='The ID of the ad client for which to generate a report')
36+
37+
MAX_PAGE_SIZE = 50
38+
39+
40+
def main(argv):
41+
# Authenticate and construct service.
42+
service, flags = sample_tools.init(
43+
argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[argparser],
44+
scope='https://www.googleapis.com/auth/adexchange.seller.readonly')
45+
46+
ad_client_id = flags.ad_client_id
47+
48+
try:
49+
# Retrieve ad unit list in pages and display data as we receive it.
50+
request = service.adunits().list(adClientId=ad_client_id,
51+
maxResults=MAX_PAGE_SIZE)
52+
53+
while request is not None:
54+
result = request.execute()
55+
ad_units = result['items']
56+
for ad_unit in ad_units:
57+
print ('Ad unit with code "%s", name "%s" and status "%s" was found. ' %
58+
(ad_unit['code'], ad_unit['name'], ad_unit['status']))
59+
60+
request = service.adunits().list_next(request, result)
61+
62+
except client.AccessTokenRefreshError:
63+
print ('The credentials have been revoked or expired, please re-run the '
64+
'application to re-authorize')
65+
66+
if __name__ == '__main__':
67+
main(sys.argv)

0 commit comments

Comments
 (0)