AdSense Management API v1.1 code samples. Reviewed in http://codereview.appspot.com/5488082/.
diff --git a/samples/adsense/generate_report.py b/samples/adsense/generate_report.py
index 576fcfd..f699a6d 100644
--- a/samples/adsense/generate_report.py
+++ b/samples/adsense/generate_report.py
@@ -17,24 +17,32 @@
"""This example retrieves a report for the specified ad client.
To get ad clients, run get_all_ad_clients.py.
+
Tags: reports.generate
"""
__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('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):
+ # Process flags and read their values.
sample_utils.process_flags(argv)
+ ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
- ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
-
try:
# Retrieve report.
result = service.reports().generate(
diff --git a/samples/adsense/generate_report_with_paging.py b/samples/adsense/generate_report_with_paging.py
index 40666b9..8217dee 100644
--- a/samples/adsense/generate_report_with_paging.py
+++ b/samples/adsense/generate_report_with_paging.py
@@ -21,12 +21,14 @@
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.
To get ad clients, run get_all_ad_clients.py.
+
Tags: reports.generate
"""
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys
+import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
@@ -34,15 +36,21 @@
# This is the maximum number of obtainable rows for paged reports.
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):
+ # Process flags and read their values.
sample_utils.process_flags(argv)
+ ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
- ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
-
try:
# Retrieve report in pages and display data as we receive it.
start_index = 0
diff --git a/samples/adsense/get_account_tree.py b/samples/adsense/get_account_tree.py
new file mode 100644
index 0000000..91b7dac
--- /dev/null
+++ b/samples/adsense/get_account_tree.py
@@ -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)
diff --git a/samples/adsense/get_all_accounts.py b/samples/adsense/get_all_accounts.py
new file mode 100644
index 0000000..36ea5f8
--- /dev/null
+++ b/samples/adsense/get_all_accounts.py
@@ -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)
diff --git a/samples/adsense/get_all_ad_clients.py b/samples/adsense/get_all_ad_clients.py
index c0d78bc..b38ab3e 100644
--- a/samples/adsense/get_all_ad_clients.py
+++ b/samples/adsense/get_all_ad_clients.py
@@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# 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
"""
@@ -38,7 +38,7 @@
# Retrieve ad client list in pages and display data as we receive it.
request = service.adclients().list(maxResults=MAX_PAGE_SIZE)
- while ( request != None ):
+ while request is not None:
result = request.execute()
ad_clients = result['items']
for ad_client in ad_clients:
diff --git a/samples/adsense/get_all_ad_clients_for_account.py b/samples/adsense/get_all_ad_clients_for_account.py
new file mode 100644
index 0000000..49ab765
--- /dev/null
+++ b/samples/adsense/get_all_ad_clients_for_account.py
@@ -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)
diff --git a/samples/adsense/get_all_ad_units.py b/samples/adsense/get_all_ad_units.py
index ee296d2..3eb6b8a 100644
--- a/samples/adsense/get_all_ad_units.py
+++ b/samples/adsense/get_all_ad_units.py
@@ -17,32 +17,40 @@
"""This example gets all ad units in an ad client.
To get ad clients, run get_all_ad_clients.py.
+
Tags: 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('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):
+ # Process flags and read their values.
sample_utils.process_flags(argv)
+ ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
- ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
-
try:
# Retrieve ad unit list in pages and display data as we receive it.
request = service.adunits().list(adClientId=ad_client_id,
maxResults=MAX_PAGE_SIZE)
- while ( request != None ):
+ while request is not None:
result = request.execute()
ad_units = result['items']
for ad_unit in ad_units:
diff --git a/samples/adsense/get_all_ad_units_for_custom_channel.py b/samples/adsense/get_all_ad_units_for_custom_channel.py
new file mode 100644
index 0000000..a4ac18f
--- /dev/null
+++ b/samples/adsense/get_all_ad_units_for_custom_channel.py
@@ -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)
diff --git a/samples/adsense/get_all_custom_channels.py b/samples/adsense/get_all_custom_channels.py
index 9386477..e43b5e8 100644
--- a/samples/adsense/get_all_custom_channels.py
+++ b/samples/adsense/get_all_custom_channels.py
@@ -17,38 +17,58 @@
"""This example gets all custom channels in an ad client.
To get ad clients, run get_all_ad_clients.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('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):
+ # Process flags and read their values.
sample_utils.process_flags(argv)
+ ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
- ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
-
try:
# Retrieve custom channel list in pages and display data as we receive it.
request = service.customchannels().list(adClientId=ad_client_id,
maxResults=MAX_PAGE_SIZE)
- while ( request != None ):
+ 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:
diff --git a/samples/adsense/get_all_custom_channels_for_ad_unit.py b/samples/adsense/get_all_custom_channels_for_ad_unit.py
new file mode 100644
index 0000000..b1dd1ef
--- /dev/null
+++ b/samples/adsense/get_all_custom_channels_for_ad_unit.py
@@ -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)
diff --git a/samples/adsense/get_all_url_channels.py b/samples/adsense/get_all_url_channels.py
index 6817ad5..cede09d 100644
--- a/samples/adsense/get_all_url_channels.py
+++ b/samples/adsense/get_all_url_channels.py
@@ -17,32 +17,40 @@
"""This example gets all URL channels in an ad client.
To get ad clients, run get_all_ad_clients.py.
+
Tags: urlchannels.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('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):
+ # Process flags and read their values.
sample_utils.process_flags(argv)
+ ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
- ad_client_id = 'INSERT_AD_CLIENT_ID_HERE'
-
try:
# Retrieve URL channel list in pages and display data as we receive it.
request = service.urlchannels().list(adClientId=ad_client_id,
maxResults=MAX_PAGE_SIZE)
- while ( request != None ):
+ while request is not None:
result = request.execute()
custom_channels = result['items']
diff --git a/samples/adsense/sample_utils.py b/samples/adsense/sample_utils.py
index e4463e5..f057e04 100644
--- a/samples/adsense/sample_utils.py
+++ b/samples/adsense/sample_utils.py
@@ -74,7 +74,7 @@
try:
argv = FLAGS(argv)
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)
# Set the logging according to the command-line flag.
@@ -98,7 +98,7 @@
"""Retrieves an AdSense Management API service 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