Removes AdSense and AdSenseHost samples (moved to GitHub)

This commit is contained in:
Jose Alcerreca
2014-04-08 13:43:48 +01:00
parent 18e9017c92
commit 34cf352fcc
40 changed files with 2 additions and 2468 deletions

View File

@@ -2,3 +2,4 @@ A collection of command-line samples for the AdSense Management API.
api: adsense
keywords: cmdline
uri: https://github.com/googleads/googleads-adsense-examples

View File

@@ -1,9 +0,0 @@
{
"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

@@ -1,89 +0,0 @@
#!/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.
"""Retrieves a saved report or a report for the specified ad client.
To get ad clients, run get_all_ad_clients.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')
argparser.add_argument(
'--report_id',
help='The ID of the saved report to generate')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
# Process flags and read their values.
ad_client_id = flags.ad_client_id
saved_report_id = flags.report_id
try:
# Retrieve report.
if saved_report_id:
result = service.reports().saved().generate(
savedReportId=saved_report_id).execute()
elif ad_client_id:
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()
else:
argparser.print_help()
sys.exit(1)
# 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
# Display date range.
print 'Report from %s to %s.' % (result['startDate'], result['endDate'])
print
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

@@ -1,100 +0,0 @@
#!/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 retrieves a report for the specified ad client.
Please only use pagination if your application requires it due to memory or
storage constraints.
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 argparse
import sys
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
# This is the maximum number of obtainable rows for paged reports.
ROW_LIMIT = 5000
# 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, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
ad_client_id = flags.ad_client_id
try:
# Retrieve report in pages and display data as we receive it.
start_index = 0
rows_to_obtain = MAX_PAGE_SIZE
while True:
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'],
startIndex=start_index,
maxResults=rows_to_obtain).execute()
# If this is the first page, display the headers.
if start_index == 0:
for header in result['headers']:
print '%25s' % header['name'],
print
# Display results for this page.
for row in result['rows']:
for column in row:
print '%25s' % column,
print
start_index += len(result['rows'])
# Check to see if we're going to go above the limit and get as many
# results as we can.
if start_index + MAX_PAGE_SIZE > ROW_LIMIT:
rows_to_obtain = ROW_LIMIT - start_index
if rows_to_obtain <= 0:
break
if start_index >= int(result['totalMatchedRows']):
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

@@ -1,71 +0,0 @@
#!/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__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags, and set them as required.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
'account_id',
help='The ID of the account to use as the root of the tree')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
# Process flags and read their values.
account_id = flags.account_id
try:
# Retrieve account.
request = service.accounts().get(accountId=account_id, tree=True)
account = request.execute()
if account:
display_tree(account)
except client.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

@@ -1,56 +0,0 @@
#!/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 apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, unused_flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
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 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

@@ -1,59 +0,0 @@
#!/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 the logged in user's default account.
Tags: adclients.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import sys
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, unused_flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
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()
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 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

@@ -1,68 +0,0 @@
#!/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__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('account_id',
help='The ID of the account for which to get ad clients')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
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()
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 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

@@ -1,68 +0,0 @@
#!/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 in an ad client.
To get ad clients, run get_all_ad_clients.py.
Tags: 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(
'ad_client_id',
help='The ID of the ad client for which to generate a report')
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
ad_client_id = flags.ad_client_id
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 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 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

@@ -1,78 +0,0 @@
#!/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__ = '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 account with the specified custom channel')
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client with the specified custom channel')
argparser.add_argument(
'custom_channel_id',
help='The ID of the custom channel for which to get ad units')
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
# Process flags and read their values.
account_id = flags.account_id
ad_client_id = flags.ad_client_id
custom_channel_id = flags.custom_channel_id
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 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

@@ -1,56 +0,0 @@
#!/usr/bin/python
#
# Copyright 2013 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.
"""Gets all alerts available for the logged in user's default account.
Tags: metadata.alerts.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import sys
from apiclient import sample_tools
from oauth2client import client
def main(argv):
# Authenticate and construct service.
service, unused_flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense')
try:
# Retrieve alerts list in pages and display data as we receive it.
request = service.alerts().list()
if request is not None:
result = request.execute()
if 'items' in result:
alerts = result['items']
for alert in alerts:
print ('Alert id "%s" with severity "%s" and type "%s" was found. '
% (alert['id'], alert['severity'], alert['type']))
# Uncomment to dismiss alert. Note that this cannot be undone.
# service.alerts().delete(alertId=alert['id']).execute()
else:
print 'No alerts 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

@@ -1,79 +0,0 @@
#!/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 in an ad client.
To get ad clients, run get_all_ad_clients.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, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
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()
custom_channels = result['items']
for custom_channel in custom_channels:
print ('Custom channel with id "%s" and name "%s" was found. '
% (custom_channel['id'], 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 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

@@ -1,91 +0,0 @@
#!/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__ = '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 account with the specified ad unit')
argparser.add_argument(
'ad_client_id',
help='The ID of the ad client with the specified ad unit')
argparser.add_argument(
'ad_unit_id',
help='The ID of the ad unit for which to get custom channels')
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
# Process flags and read their values.
account_id = flags.account_id
ad_client_id = flags.ad_client_id
ad_unit_id = flags.ad_unit_id
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 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

@@ -1,54 +0,0 @@
#!/usr/bin/python
#
# Copyright 2013 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.
"""Gets all dimensions available for the logged in user's default account.
Tags: metadata.dimensions.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import sys
from apiclient import sample_tools
from oauth2client import client
def main(argv):
# Authenticate and construct service.
service, unused_flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
# Retrieve metrics list in pages and display data as we receive it.
request = service.metadata().dimensions().list()
if request is not None:
result = request.execute()
if 'items' in result:
dimensions = result['items']
for dimension in dimensions:
print ('Dimension id "%s" for product(s): [%s] was found. '
% (dimension['id'], ', '.join(dimension['supportedProducts'])))
else:
print 'No dimensions 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

@@ -1,54 +0,0 @@
#!/usr/bin/python
#
# Copyright 2013 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.
"""Gets all metrics available for the logged in user's default account.
Tags: metadata.metrics.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import sys
from apiclient import sample_tools
from oauth2client import client
def main(argv):
# Authenticate and construct service.
service, unused_flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
# Retrieve metrics list in pages and display data as we receive it.
request = service.metadata().metrics().list()
if request is not None:
result = request.execute()
if 'items' in result:
metrics = result['items']
for metric in metrics:
print ('Metric id "%s" for product(s): [%s] was found. '
% (metric['id'], ', '.join(metric['supportedProducts'])))
else:
print 'No metrics 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

@@ -1,55 +0,0 @@
#!/usr/bin/python
#
# Copyright 2013 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.
"""Gets all payments available for the logged in user's default account.
Tags: payments.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import sys
from apiclient import sample_tools
from oauth2client import client
def main(argv):
# Authenticate and construct service.
service, unused_flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
# Retrieve payments list in pages and display data as we receive it.
request = service.payments().list()
if request is not None:
result = request.execute()
print result
if 'items' in result:
payments = result['items']
for payment in payments:
print ('Payment with id "%s" of %s %s and date %s was found. '
% (str(payment['id']),
payment['paymentAmount'],
payment['paymentAmountCurrencyCode'],
payment.get('paymentDate', 'unknown')))
else:
print 'No payments 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

@@ -1,63 +0,0 @@
#!/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.
"""Gets all the saved ad styles for the user's default account.
Tags: savedadstyles.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import sys
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, unused_flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
# Retrieve ad client list in pages and display data as we receive it.
request = service.savedadstyles().list(maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
saved_ad_styles = result['items']
for saved_ad_style in saved_ad_styles:
print ('Saved ad style with ID "%s" and background color "#%s" was '
'found.'
% (saved_ad_style['id'],
saved_ad_style['adStyle']['colors']['background']))
if ('corners' in saved_ad_style['adStyle']
and 'font' in saved_ad_style['adStyle']):
print ('It has "%s" corners and a "%s" size font.' %
(saved_ad_style['adStyle']['corners'],
saved_ad_style['adStyle']['font']['size']))
request = service.savedadstyles().list_next(request, result)
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

@@ -1,56 +0,0 @@
#!/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.
"""Gets all the saved reports for user's default account.
Tags: savedreports.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import sys
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
def main(argv):
# Authenticate and construct service.
service, unused_flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
# Retrieve ad client list in pages and display data as we receive it.
request = service.reports().saved().list(maxResults=MAX_PAGE_SIZE)
while request is not None:
result = request.execute()
saved_reports = result['items']
for saved_report in saved_reports:
print ('Saved ad style with ID "%s" and name "%s" was found.'
% (saved_report['id'], saved_report['name']))
request = service.reports().saved().list_next(request, result)
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

@@ -1,68 +0,0 @@
#!/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 URL channels in an ad client.
To get ad clients, run get_all_ad_clients.py.
Tags: urlchannels.list
"""
__author__ = 'jalc@google.com (Jose Alcerreca)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
# 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')
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser],
scope='https://www.googleapis.com/auth/adsense.readonly')
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()
url_channels = result['items']
for url_channel in url_channels:
print ('URL channel with URL pattern "%s" was found.'
% url_channel['urlPattern'])
request = service.customchannels().list_next(request, result)
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

@@ -2,3 +2,4 @@ A collection of command-line samples for the AdSense Host API.
api: adsensehost
keywords: cmdline
uri: https://github.com/googleads/googleads-adsensehost-examples

View File

@@ -1,92 +0,0 @@
#!/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

@@ -1,64 +0,0 @@
#!/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

@@ -1,67 +0,0 @@
#!/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

@@ -1,9 +0,0 @@
{
"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

@@ -1,68 +0,0 @@
#!/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

@@ -1,63 +0,0 @@
#!/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

@@ -1,63 +0,0 @@
#!/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

@@ -1,75 +0,0 @@
#!/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

@@ -1,83 +0,0 @@
#!/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

@@ -1,61 +0,0 @@
#!/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

@@ -1,66 +0,0 @@
#!/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

@@ -1,71 +0,0 @@
#!/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

@@ -1,77 +0,0 @@
#!/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

@@ -1,72 +0,0 @@
#!/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

@@ -1,70 +0,0 @@
#!/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

@@ -1,40 +0,0 @@
#!/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

@@ -1,55 +0,0 @@
#!/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

@@ -1,72 +0,0 @@
#!/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

@@ -1,70 +0,0 @@
#!/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

@@ -1,56 +0,0 @@
#!/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)