AdSense Management API v1.1 code samples. Reviewed in http://codereview.appspot.com/5488082/.

This commit is contained in:
Joe Gregorio
2012-02-07 14:31:01 -05:00
parent 6f11ea1ca4
commit 579c8c94e8
12 changed files with 433 additions and 17 deletions

View File

@@ -17,24 +17,32 @@
"""This example retrieves a report for the specified ad client. """This example retrieves a report for the specified ad client.
To get ad clients, run get_all_ad_clients.py. To get ad clients, run get_all_ad_clients.py.
Tags: reports.generate Tags: reports.generate
""" """
__author__ = 'sergio.gomes@google.com (Sergio Gomes)' __author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys import sys
import gflags
from oauth2client.client import AccessTokenRefreshError from oauth2client.client import AccessTokenRefreshError
import sample_utils import sample_utils
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ID of the ad client for which to generate a report',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
def main(argv): def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv) sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service. # Authenticate and construct service.
service = sample_utils.initialize_service() service = sample_utils.initialize_service()
ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
try: try:
# Retrieve report. # Retrieve report.
result = service.reports().generate( result = service.reports().generate(

View File

@@ -21,12 +21,14 @@ storage constraints.
If you need to retrieve more than 5000 rows, please check generate_report.py, as If you need to retrieve more than 5000 rows, please check generate_report.py, as
due to current limitations you will not be able to use paging for large reports. due to current limitations you will not be able to use paging for large reports.
To get ad clients, run get_all_ad_clients.py. To get ad clients, run get_all_ad_clients.py.
Tags: reports.generate Tags: reports.generate
""" """
__author__ = 'sergio.gomes@google.com (Sergio Gomes)' __author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys import sys
import gflags
from oauth2client.client import AccessTokenRefreshError from oauth2client.client import AccessTokenRefreshError
import sample_utils import sample_utils
@@ -34,15 +36,21 @@ MAX_PAGE_SIZE = 50
# This is the maximum number of obtainable rows for paged reports. # This is the maximum number of obtainable rows for paged reports.
ROW_LIMIT = 5000 ROW_LIMIT = 5000
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ID of the ad client for which to generate a report',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
def main(argv): def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv) sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service. # Authenticate and construct service.
service = sample_utils.initialize_service() service = sample_utils.initialize_service()
ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
try: try:
# Retrieve report in pages and display data as we receive it. # Retrieve report in pages and display data as we receive it.
start_index = 0 start_index = 0

View File

@@ -0,0 +1,69 @@
#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# 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 example gets a specific account for the logged in user.
This includes the full tree of sub-accounts.
Tags: accounts.get
"""
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('account_id', None,
'The ID of the account to use as the root of the tree',
short_name='a')
gflags.MarkFlagAsRequired('account_id')
def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv)
account_id = gflags.FLAGS.account_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
try:
# Retrieve account.
request = service.accounts().get(accountId=account_id, tree=True)
account = request.execute()
if account:
display_tree(account)
except AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
def display_tree(account, level=0):
print (' ' * level * 2 +
'Account with ID "%s" and name "%s" was found. ' %
(account['id'], account['name']))
if 'subAccounts' in account:
for sub_account in account['subAccounts']:
display_tree(sub_account, level + 1)
if __name__ == '__main__':
main(sys.argv)

View File

@@ -0,0 +1,55 @@
#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# 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 example gets all accounts for the logged in user.
Tags: accounts.list
"""
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys
from oauth2client.client import AccessTokenRefreshError
import sample_utils
MAX_PAGE_SIZE = 50
def main(argv):
sample_utils.process_flags(argv)
# Authenticate and construct service.
service = sample_utils.initialize_service()
try:
# Retrieve account list in pages and display data as we receive it.
request = service.accounts().list(maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
accounts = result['items']
for account in accounts:
print ('Account with ID "%s" and name "%s" was found. '
% (account['id'], account['name']))
request = service.accounts().list_next(request, result)
except AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
if __name__ == '__main__':
main(sys.argv)

View File

@@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""This example gets all ad clients for an account. """This example gets all ad clients for the logged in user's default account.
Tags: adclients.list Tags: adclients.list
""" """
@@ -38,7 +38,7 @@ def main(argv):
# Retrieve ad client list in pages and display data as we receive it. # Retrieve ad client list in pages and display data as we receive it.
request = service.adclients().list(maxResults=MAX_PAGE_SIZE) request = service.adclients().list(maxResults=MAX_PAGE_SIZE)
while ( request != None ): while request is not None:
result = request.execute() result = request.execute()
ad_clients = result['items'] ad_clients = result['items']
for ad_client in ad_clients: for ad_client in ad_clients:

View File

@@ -0,0 +1,67 @@
#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# 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 example gets all ad clients for an account.
Tags: accounts.adclients.list
"""
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys
from oauth2client.client import AccessTokenRefreshError
import sample_utils
MAX_PAGE_SIZE = 50
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('account_id', None,
'The ID of the account for which to get ad clients',
short_name='a')
gflags.MarkFlagAsRequired('account_id')
def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv)
account_id = gflags.FLAGS.account_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
try:
# Retrieve ad client list in pages and display data as we receive it.
request = service.accounts().adclients().list(accountId=account_id,
maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
ad_clients = result['items']
for ad_client in ad_clients:
print ('Ad client for product "%s" with ID "%s" was found. '
% (ad_client['productCode'], ad_client['id']))
print ('\tSupports reporting: %s' %
(ad_client['supportsReporting'] and 'Yes' or 'No'))
request = service.adclients().list_next(request, result)
except AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
if __name__ == '__main__':
main(sys.argv)

View File

@@ -17,32 +17,40 @@
"""This example gets all ad units in an ad client. """This example gets all ad units in an ad client.
To get ad clients, run get_all_ad_clients.py. To get ad clients, run get_all_ad_clients.py.
Tags: adunits.list Tags: adunits.list
""" """
__author__ = 'sergio.gomes@google.com (Sergio Gomes)' __author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys import sys
import gflags
from oauth2client.client import AccessTokenRefreshError from oauth2client.client import AccessTokenRefreshError
import sample_utils import sample_utils
MAX_PAGE_SIZE = 50 MAX_PAGE_SIZE = 50
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ad client ID for which to get ad units',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
def main(argv): def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv) sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service. # Authenticate and construct service.
service = sample_utils.initialize_service() service = sample_utils.initialize_service()
ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
try: try:
# Retrieve ad unit list in pages and display data as we receive it. # Retrieve ad unit list in pages and display data as we receive it.
request = service.adunits().list(adClientId=ad_client_id, request = service.adunits().list(adClientId=ad_client_id,
maxResults=MAX_PAGE_SIZE) maxResults=MAX_PAGE_SIZE)
while ( request != None ): while request is not None:
result = request.execute() result = request.execute()
ad_units = result['items'] ad_units = result['items']
for ad_unit in ad_units: for ad_unit in ad_units:

View File

@@ -0,0 +1,80 @@
#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# 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 example gets all ad units corresponding to a specified custom channel.
To get custom channels, run get_all_custom_channels.py.
Tags: accounts.customchannels.adunits.list
"""
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
MAX_PAGE_SIZE = 50
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('account_id', None,
'The ID of the account with the specified custom channel',
short_name='a')
gflags.MarkFlagAsRequired('account_id')
gflags.DEFINE_string('ad_client_id', None,
'The ID of the ad client with the specified custom channel',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
gflags.DEFINE_string('custom_channel_id', None,
'The ID of the custom channel for which to get ad units',
short_name='x')
gflags.MarkFlagAsRequired('custom_channel_id')
def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv)
account_id = gflags.FLAGS.account_id
ad_client_id = gflags.FLAGS.ad_client_id
custom_channel_id = gflags.FLAGS.custom_channel_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
try:
# Retrieve ad unit list in pages and display data as we receive it.
request = service.accounts().customchannels().adunits().list(
accountId=account_id, adClientId=ad_client_id,
customChannelId=custom_channel_id, maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
ad_units = result['items']
for ad_unit in ad_units:
print ('Ad unit with code "%s", name "%s" and status "%s" was found. ' %
(ad_unit['code'], ad_unit['name'], ad_unit['status']))
request = service.adunits().list_next(request, result)
except AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
if __name__ == '__main__':
main(sys.argv)

View File

@@ -17,38 +17,58 @@
"""This example gets all custom channels in an ad client. """This example gets all custom channels in an ad client.
To get ad clients, run get_all_ad_clients.py. To get ad clients, run get_all_ad_clients.py.
Tags: customchannels.list Tags: customchannels.list
""" """
__author__ = 'sergio.gomes@google.com (Sergio Gomes)' __author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys import sys
import gflags
from oauth2client.client import AccessTokenRefreshError from oauth2client.client import AccessTokenRefreshError
import sample_utils import sample_utils
MAX_PAGE_SIZE = 50 MAX_PAGE_SIZE = 50
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ad client ID for which to get custom channels',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
def main(argv): def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv) sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service. # Authenticate and construct service.
service = sample_utils.initialize_service() service = sample_utils.initialize_service()
ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
try: try:
# Retrieve custom channel list in pages and display data as we receive it. # Retrieve custom channel list in pages and display data as we receive it.
request = service.customchannels().list(adClientId=ad_client_id, request = service.customchannels().list(adClientId=ad_client_id,
maxResults=MAX_PAGE_SIZE) maxResults=MAX_PAGE_SIZE)
while ( request != None ): while request is not None:
result = request.execute() result = request.execute()
custom_channels = result['items'] custom_channels = result['items']
for custom_channel in custom_channels: for custom_channel in custom_channels:
print ('Custom channel with code "%s" and name "%s" was found. ' print ('Custom channel with code "%s" and name "%s" was found. '
% (custom_channel['code'], custom_channel['name'])) % (custom_channel['code'], custom_channel['name']))
if 'targetingInfo' in custom_channel:
print ' Targeting info:'
targeting_info = custom_channel['targetingInfo']
if 'adsAppearOn' in targeting_info:
print ' Ads appear on: %s' % targeting_info['adsAppearOn']
if 'location' in targeting_info:
print ' Location: %s' % targeting_info['location']
if 'description' in targeting_info:
print ' Description: %s' % targeting_info['description']
if 'siteLanguage' in targeting_info:
print ' Site language: %s' % targeting_info['siteLanguage']
request = service.customchannels().list_next(request, result) request = service.customchannels().list_next(request, result)
except AccessTokenRefreshError: except AccessTokenRefreshError:

View File

@@ -0,0 +1,93 @@
#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# 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 example gets all custom channels an ad unit has been added to.
To get ad clients, run get_all_ad_clients.py. To get ad units, run
get_all_ad_units.py.
Tags: customchannels.list
"""
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
MAX_PAGE_SIZE = 50
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('account_id', None,
'The ID of the account with the specified ad unit',
short_name='a')
gflags.MarkFlagAsRequired('account_id')
gflags.DEFINE_string('ad_client_id', None,
'The ID of the ad client with the specified ad unit',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
gflags.DEFINE_string('ad_unit_id', None,
'The ID of the ad unit for which to get custom channels',
short_name='u')
gflags.MarkFlagAsRequired('ad_unit_id')
def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv)
account_id = gflags.FLAGS.account_id
ad_client_id = gflags.FLAGS.ad_client_id
ad_unit_id = gflags.FLAGS.ad_unit_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
try:
# Retrieve custom channel list in pages and display data as we receive it.
request = service.accounts().adunits().customchannels().list(
accountId=account_id, adClientId=ad_client_id, adUnitId=ad_unit_id,
maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
custom_channels = result['items']
for custom_channel in custom_channels:
print ('Custom channel with code "%s" and name "%s" was found. '
% (custom_channel['code'], custom_channel['name']))
if 'targetingInfo' in custom_channel:
print ' Targeting info:'
targeting_info = custom_channel['targetingInfo']
if 'adsAppearOn' in targeting_info:
print ' Ads appear on: %s' % targeting_info['adsAppearOn']
if 'location' in targeting_info:
print ' Location: %s' % targeting_info['location']
if 'description' in targeting_info:
print ' Description: %s' % targeting_info['description']
if 'siteLanguage' in targeting_info:
print ' Site language: %s' % targeting_info['siteLanguage']
request = service.customchannels().list_next(request, result)
except AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
if __name__ == '__main__':
main(sys.argv)

View File

@@ -17,32 +17,40 @@
"""This example gets all URL channels in an ad client. """This example gets all URL channels in an ad client.
To get ad clients, run get_all_ad_clients.py. To get ad clients, run get_all_ad_clients.py.
Tags: urlchannels.list Tags: urlchannels.list
""" """
__author__ = 'sergio.gomes@google.com (Sergio Gomes)' __author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys import sys
import gflags
from oauth2client.client import AccessTokenRefreshError from oauth2client.client import AccessTokenRefreshError
import sample_utils import sample_utils
MAX_PAGE_SIZE = 50 MAX_PAGE_SIZE = 50
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ad client ID for which to get URL channels',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
def main(argv): def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv) sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service. # Authenticate and construct service.
service = sample_utils.initialize_service() service = sample_utils.initialize_service()
ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
try: try:
# Retrieve URL channel list in pages and display data as we receive it. # Retrieve URL channel list in pages and display data as we receive it.
request = service.urlchannels().list(adClientId=ad_client_id, request = service.urlchannels().list(adClientId=ad_client_id,
maxResults=MAX_PAGE_SIZE) maxResults=MAX_PAGE_SIZE)
while ( request != None ): while request is not None:
result = request.execute() result = request.execute()
custom_channels = result['items'] custom_channels = result['items']

View File

@@ -74,7 +74,7 @@ def process_flags(argv):
try: try:
argv = FLAGS(argv) argv = FLAGS(argv)
except gflags.FlagsError, e: except gflags.FlagsError, e:
print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS)
sys.exit(1) sys.exit(1)
# Set the logging according to the command-line flag. # Set the logging according to the command-line flag.
@@ -98,7 +98,7 @@ def retrieve_service(http):
"""Retrieves an AdSense Management API service via the discovery service.""" """Retrieves an AdSense Management API service via the discovery service."""
# Construct a service object via the discovery service. # Construct a service object via the discovery service.
service = build("adsense", "v1", http=http) service = build("adsense", "v1.1", http=http)
return service return service