Added AdSense Host API v4.1 samples.

Reviewed in https://codereview.appspot.com/7749046/.

Index: samples/adsensehost/README
===================================================================
new file mode 100644
This commit is contained in:
Joe Gregorio
2014-03-13 09:32:27 -04:00
parent 956c120654
commit 18e9017c92
21 changed files with 1298 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
A collection of command-line samples for the AdSense Host API.
api: adsensehost
keywords: cmdline

View File

@@ -0,0 +1,92 @@
#!/usr/bin/python
#
# Copyright 2012 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 adds a new ad unit to a publisher ad client.
To get ad clients, run get_all_ad_clients_for_publisher.py.
Tags: accounts.adunits.insert
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
from sample_utils import GetUniqueName
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'account_id',
help='The ID of the pub account on which to create the ad unit')
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client on which to create the ad unit')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
account_id = flags.account_id
ad_client_id = flags.ad_client_id
try:
ad_unit = {
'name': 'Ad Unit #%s' % GetUniqueName(),
'contentAdsSettings': {
'backupOption': {
'type': 'COLOR',
'color': 'ffffff'
},
'size': 'SIZE_200_200',
'type': 'TEXT'
},
'customStyle': {
'colors': {
'background': 'ffffff',
'border': '000000',
'text': '000000',
'title': '000000',
'url': '0000ff'
},
'corners': 'SQUARE',
'font': {
'family': 'ACCOUNT_DEFAULT_FAMILY',
'size': 'ACCOUNT_DEFAULT_SIZE'
}
}
}
# Create ad unit.
request = service.accounts().adunits().insert(adClientId=ad_client_id,
accountId=account_id,
body=ad_unit)
result = request.execute()
print ('Ad unit of type "%s", name "%s" and status "%s" was created.' %
(result['contentAdsSettings']['type'], result['name'],
result['status']))
except client.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

@@ -0,0 +1,64 @@
#!/usr/bin/python
#
# Copyright 2012 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 adds a custom channel to a host ad client.
To get ad clients, run get_all_ad_clients_for_host.py.
Tags: customchannels.insert
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
from sample_utils import GetUniqueName
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client on which to create the custom channel')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
try:
custom_channel = {
'name': 'Sample Channel #%s' % GetUniqueName()
}
# Add custom channel.
request = service.customchannels().insert(adClientId=ad_client_id,
body=custom_channel)
result = request.execute()
print ('Custom channel with id "%s", code "%s" and name "%s" was created.'
% (result['id'], result['code'], result['name']))
except client.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

@@ -0,0 +1,67 @@
#!/usr/bin/python
#
# Copyright 2012 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 adds a URL channel to a host ad client.
To get ad clients, run get_all_ad_clients_for_host.py.
Tags: urlchannels.insert
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client on which to create the URL channel')
argparser.add_argument(
'url_pattern',
help='The URL pattern for the new custom channel')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
url_pattern = flags.url_pattern
try:
custom_channel = {
'urlPattern': url_pattern
}
# Add URL channel.
request = service.urlchannels().insert(adClientId=ad_client_id,
body=custom_channel)
result = request.execute()
print ('URL channel with id "%s" and URL pattern "%s" was created.' %
(result['id'], result['urlPattern']))
except client.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

@@ -0,0 +1,9 @@
{
"installed": {
"client_id": "[[INSERT CLIENT ID HERE]]",
"client_secret": "[[INSERT CLIENT SECRET HERE]]",
"redirect_uris": [],
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token"
}
}

View File

@@ -0,0 +1,68 @@
#!/usr/bin/python
#
# Copyright 2012 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 deletes an ad unit on a publisher ad client.
To get ad clients, run get_all_ad_clients_for_publisher.py.
To get ad units, run get_all_ad_units_for_publisher.py.
Tags: accounts.adunits.delete
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'account_id',
help='The ID of the pub account on which the ad unit exists')
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client on which the ad unit exists')
argparser.add_argument(
'ad_unit_id',
help='The ID of the ad unit to be deleted')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
account_id = flags.account_id
ad_client_id = flags.ad_client_id
ad_unit_id = flags.ad_unit_id
try:
# Delete ad unit.
request = service.accounts().adunits().delete(accountId=account_id,
adClientId=ad_client_id,
adUnitId=ad_unit_id)
result = request.execute()
print 'Ad unit with ID "%s" was deleted.' % result['id']
except client.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

@@ -0,0 +1,63 @@
#!/usr/bin/python
#
# Copyright 2012 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 deletes a custom channel on a host ad client.
To get ad clients, run get_all_ad_clients_for_host.py.
To get custom channels, run get_all_custom_channels_for_host.py.
Tags: customchannels.delete
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'ad_client_id',
help='The ad client ID that contains the custom channel')
argparser.add_argument(
'custom_channel_id',
help='The ID of the custom channel to be deleted')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
custom_channel_id = flags.custom_channel_id
try:
# Delete custom channel.
request = service.customchannels().delete(
adClientId=ad_client_id, customChannelId=custom_channel_id)
result = request.execute()
print 'Custom channel with ID "%s" was deleted.' % result['id']
except client.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

@@ -0,0 +1,63 @@
#!/usr/bin/python
#
# Copyright 2012 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 deletes a URL channel on a host ad client.
To get ad clients, run get_all_ad_clients_for_host.py.
To get URL channels, run get_all_url_channels_for_host.py.
Tags: urlchannels.delete
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'ad_client_id',
help='The ad client ID that contains the URL channel')
argparser.add_argument(
'url_channel_id',
help='The ID of the URL channel to be deleted')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
url_channel_id = flags.url_channel_id
try:
# Delete URL channel.
request = service.urlchannels().delete(adClientId=ad_client_id,
urlChannelId=url_channel_id)
result = request.execute()
print 'URL channel with ID "%s" was deleted.' % result['id']
except client.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

@@ -0,0 +1,75 @@
#!/usr/bin/python
#
# Copyright 2012 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 retrieves a report for the specified host ad client.
To get ad clients, run get_all_ad_clients_for_host.py.
Tags: reports.generate
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client for which to generate a report')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
try:
# Retrieve report.
result = service.reports().generate(
startDate='2011-01-01',
endDate='2011-08-31',
filter=['AD_CLIENT_ID==' + ad_client_id],
metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
'AD_REQUESTS_RPM', 'EARNINGS'],
dimension=['DATE'],
sort=['+DATE']).execute()
if 'rows' in result:
# Display headers.
for header in result['headers']:
print '%25s' % header['name'],
print
# Display results.
for row in result['rows']:
for column in row:
print '%25s' % column,
print
else:
print 'No rows returned.'
except client.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

@@ -0,0 +1,83 @@
#!/usr/bin/python
#
# Copyright 2012 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 retrieves a report for the specified publisher ad client.
Note that the statistics returned in these reports only include data from ad
units created with the AdSense Host API v4.x.
To create ad units, run add_ad_unit_to_publisher.py.
To get ad clients, run get_all_ad_clients_for_publisher.py.
Tags: accounts.reports.generate
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'account_id',
help='The ID of the publisher account for which to generate a report')
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client for which to generate a report')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
account_id = flags.account_id
try:
# Retrieve report.
result = service.accounts().reports().generate(
accountId=account_id,
startDate='2011-01-01',
endDate='2011-08-31',
filter=['AD_CLIENT_ID==' + ad_client_id],
metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
'AD_REQUESTS_RPM', 'EARNINGS'],
dimension=['DATE'],
sort=['+DATE']).execute()
if 'rows' in result:
# Display headers.
for header in result['headers']:
print '%25s' % header['name'],
print
# Display results.
for row in result['rows']:
for column in row:
print '%25s' % column,
print
else:
print 'No rows returned.'
except client.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

@@ -0,0 +1,61 @@
#!/usr/bin/python
#
# Copyright 2012 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 the account data for a publisher from their ad client ID.
Tags: accounts.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'ad_client_id',
help='The publisher ad client ID for which to get the data')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
try:
# Retrieve account list in pages and display data as we receive it.
request = service.accounts().list(filterAdClientId=ad_client_id)
result = request.execute()
if 'items' in result:
accounts = result['items']
for account in accounts:
print ('Account with ID "%s", name "%s" and status "%s" was found. ' %
(account['id'], account['name'], account['status']))
else:
print 'No accounts were found.'
except client.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

@@ -0,0 +1,66 @@
#!/usr/bin/python
#
# Copyright 2012 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 the ad clients in the host account.
Tags: adclients.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, _ = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
try:
# Retrieve ad client list in pages and display data as we receive it.
request = service.adclients().list(maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
if 'items' in result:
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' %
'Yes' if ad_client['supportsReporting'] else 'No')
request = service.adclients().list_next(request, result)
else:
print 'No ad clients were found.'
break
except client.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

@@ -0,0 +1,71 @@
#!/usr/bin/python
#
# Copyright 2012 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 a publisher account.
Tags: accounts.adclients.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'account_id',
help='The ID of the publisher account for which to get ad clients')
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
account_id = flags.account_id
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()
if 'items' in result:
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' %
'Yes' if ad_client['supportsReporting'] else 'No')
request = service.adclients().list_next(request, result)
else:
print 'No ad clients were found.'
break
except client.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

@@ -0,0 +1,77 @@
#!/usr/bin/python
#
# Copyright 2012 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 in a publisher ad client.
To get ad clients, run get_all_ad_clients_for_publisher.py.
Tags: accounts.adunits.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'account_id',
help='The ID of the pub account on which the ad unit exists')
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client on which the ad unit exists')
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
account_id = flags.account_id
try:
# Retrieve ad unit list in pages and display data as we receive it.
request = service.accounts().adunits().list(adClientId=ad_client_id,
accountId=account_id,
maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
if 'items' in result:
ad_units = result['items']
for ad_unit in ad_units:
print ('Ad unit with ID "%s", code "%s", name "%s" and status "%s" '
'was found.' %
(ad_unit['id'], ad_unit['code'], ad_unit['name'],
ad_unit['status']))
request = service.accounts().adunits().list_next(request, result)
else:
print 'No ad units were found.'
break
except client.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

@@ -0,0 +1,72 @@
#!/usr/bin/python
#
# Copyright 2012 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 in a host ad client.
To get ad clients, run get_all_ad_clients_for_host.py.
Tags: customchannels.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'ad_client_id',
help='The ad client ID for which to get custom channels')
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
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 is not None:
result = request.execute()
if 'items' in result:
custom_channels = result['items']
for custom_channel in custom_channels:
print ('Custom channel with ID "%s", code "%s" and name "%s" found.'
% (custom_channel['id'], custom_channel['code'],
custom_channel['name']))
request = service.customchannels().list_next(request, result)
else:
print 'No custom channels were found.'
break
except client.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

@@ -0,0 +1,70 @@
#!/usr/bin/python
#
# Copyright 2012 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 URL channels in a host ad client.
To get ad clients, run get_all_ad_clients_for_host.py.
Tags: urlchannels.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'ad_client_id',
help='The ad client ID for which to get URL channels')
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
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 is not None:
result = request.execute()
if 'items' in result:
url_channels = result['items']
for url_channel in url_channels:
print ('URL channel with ID "%s" and URL pattern "%s" was found.'
% (url_channel['id'], url_channel['urlPattern']))
request = service.customchannels().list_next(request, result)
else:
print 'No URL channels were found.'
break
except client.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

@@ -0,0 +1,40 @@
#!/usr/bin/python
#
# Copyright 2012 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.
"""Auxiliary file for AdSense Host API code samples."""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import datetime
def GetUniqueName(max_len=None):
"""Returns a unique value to append to various properties in the samples.
Shamelessly stolen from http://code.google.com/p/google-api-ads-python.
Args:
max_len: int Maximum length for the unique name.
Returns:
str Unique name.
"""
dt = datetime.datetime.now()
name = '%s%s%s%s%s%s%s' % (dt.microsecond, dt.second, dt.minute, dt.hour,
dt.day, dt.month, dt.year)
if max_len > len(name):
max_len = len(name)
return name[:max_len]

View File

@@ -0,0 +1,55 @@
#!/usr/bin/python
#
# Copyright 2012 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 starts an association session.
Tags: associationsessions.start
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
from sample_utils import GetUniqueName
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
def main(argv):
# Authenticate and construct service.
service, _ = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
try:
# Request a new association session.
request = service.associationsessions().start(
productCode='AFC',
websiteUrl='www.example.com/blog%s' % GetUniqueName())
result = request.execute()
print ('Association with ID "%s" and redirect URL "%s" was started.' %
(result['id'], result['redirectUrl']))
except client.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

@@ -0,0 +1,72 @@
#!/usr/bin/python
#
# Copyright 2012 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 updates an ad unit on a publisher ad client.
To get ad clients, run get_all_ad_clients_for_publisher.py.
To get ad units, run get_all_ad_units_for_publisher.py.
Tags: accounts.adunits.patch
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'account_id',
help='The ID of the publisher account on which the ad unit exists')
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client on which the ad unit exists')
argparser.add_argument(
'ad_unit_id',
help='The ID of the ad unit to be updated')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
account_id = flags.account_id
ad_client_id = flags.ad_client_id
ad_unit_id = flags.ad_unit_id
try:
ad_unit = {'customStyle': {'colors': {'text': 'ff0000'}}}
# Update ad unit text color.
request = service.accounts().adunits().patch(accountId=account_id,
adClientId=ad_client_id,
adUnitId=ad_unit_id,
body=ad_unit)
result = request.execute()
print ('Ad unit with ID "%s" was updated with text color "%s".'
% (result['id'], result['customStyle']['colors']['text']))
except client.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

@@ -0,0 +1,70 @@
#!/usr/bin/python
#
# Copyright 2012 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 updates a custom channel on a host ad client.
To get ad clients, run get_all_ad_clients_for_host.py.
To get custom channels, run get_all_custom_channels_for_host.py.
Tags: customchannels.patch
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
from sample_utils import GetUniqueName
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'ad_client_id',
help='The ad client ID that contains the custom channel')
argparser.add_argument(
'custom_channel_id',
help='The ID of the custom channel to be updated')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
ad_client_id = flags.ad_client_id
custom_channel_id = flags.custom_channel_id
try:
custom_channel = {
'name': 'Updated Sample Channel #%s' % GetUniqueName()
}
# Update custom channel.
request = service.customchannels().patch(adClientId=ad_client_id,
customChannelId=custom_channel_id,
body=custom_channel)
result = request.execute()
print ('Custom channel with ID "%s" was updated with name "%s".'
% (result['id'], result['name']))
except client.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

@@ -0,0 +1,56 @@
#!/usr/bin/python
#
# Copyright 2012 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 verifies an association session callback token.
Tags: associationsessions.verify
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'token',
help='The token returned in the association callback')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
token = flags.token
try:
# Verify the association session token.
request = service.associationsessions().verify(token=token)
result = request.execute()
print ('Association for account "%s" has status "%s" and ID "%s".' %
(result['accountId'], result['status'], result['id']))
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
if __name__ == '__main__':
main(sys.argv)