Update adsense samples to use apiclient.sample_tools.

Reviewed in https://codereview.appspot.com/8249044/.
This commit is contained in:
Joe Gregorio
2013-05-20 16:05:23 -04:00
parent 5f8e4fcc42
commit 80f68bf2ba
14 changed files with 180 additions and 296 deletions

View File

@@ -23,28 +23,29 @@ Tags: reports.generate
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import argparse
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ID of the ad client for which to generate a report',
short_name='c')
gflags.DEFINE_string('report_id', None,
'The ID of the saved report to generate',
short_name='r')
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):
# Process flags and read their values.
sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
saved_report_id = gflags.FLAGS.report_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -75,7 +76,7 @@ def main(argv):
print '%25s' % column,
print
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -27,29 +27,29 @@ Tags: reports.generate
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import argparse
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
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, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ID of the ad client for which to generate a report',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
# 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):
# Process flags and read their values.
sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -91,7 +91,7 @@ def main(argv):
if (start_index >= int(result['totalMatchedRows'])):
break
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -23,25 +23,26 @@ Tags: accounts.get
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import argparse
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
from apiclient import sample_tools
from oauth2client import client
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('account_id', None,
'The ID of the account to use as the root of the tree',
short_name='a')
gflags.MarkFlagAsRequired('account_id')
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):
# Process flags and read their values.
sample_utils.process_flags(argv)
account_id = gflags.FLAGS.account_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -51,7 +52,7 @@ def main(argv):
if account:
display_tree(account)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -22,17 +22,18 @@ Tags: accounts.list
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys
from oauth2client.client import AccessTokenRefreshError
import sample_utils
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
def main(argv):
sample_utils.process_flags(argv)
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __doc__, __file__, parents=[],
scope='https://www.googleapis.com/auth/adsense.readonly')
try:
# Retrieve account list in pages and display data as we receive it.
@@ -47,7 +48,7 @@ def main(argv):
request = service.accounts().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -22,17 +22,18 @@ Tags: adclients.list
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import sys
from oauth2client.client import AccessTokenRefreshError
import sample_utils
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
def main(argv):
sample_utils.process_flags(argv)
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -50,7 +51,7 @@ def main(argv):
request = service.adclients().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -21,26 +21,27 @@ Tags: accounts.adclients.list
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import argparse
import sys
from oauth2client.client import AccessTokenRefreshError
import sample_utils
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('account_id', None,
'The ID of the account for which to get ad clients',
short_name='a')
gflags.MarkFlagAsRequired('account_id')
# 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):
# Process flags and read their values.
sample_utils.process_flags(argv)
account_id = gflags.FLAGS.account_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -59,7 +60,7 @@ def main(argv):
request = service.adclients().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -23,27 +23,27 @@ Tags: adunits.list
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import argparse
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
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
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ad client ID for which to get ad units',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -59,7 +59,7 @@ def main(argv):
request = service.adunits().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -23,39 +23,34 @@ Tags: accounts.customchannels.adunits.list
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import argparse
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
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 for which to generate a report')
argparser.add_argument('custom_channel_id',
help='The ID of the custom channel for which to get ad units')
MAX_PAGE_SIZE = 50
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('account_id', None,
'The ID of the account with the specified custom channel',
short_name='a')
gflags.MarkFlagAsRequired('account_id')
gflags.DEFINE_string('ad_client_id', None,
'The ID of the ad client with the specified custom channel',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
gflags.DEFINE_string('custom_channel_id', None,
'The ID of the custom channel for which to get ad units',
short_name='x')
gflags.MarkFlagAsRequired('custom_channel_id')
def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv)
account_id = gflags.FLAGS.account_id
ad_client_id = gflags.FLAGS.ad_client_id
custom_channel_id = gflags.FLAGS.custom_channel_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -72,7 +67,7 @@ def main(argv):
request = service.adunits().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -23,27 +23,27 @@ Tags: customchannels.list
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import argparse
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
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
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ad client ID for which to get custom channels',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -71,7 +71,7 @@ def main(argv):
request = service.customchannels().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -24,39 +24,38 @@ Tags: customchannels.list
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import argparse
import sys
from apiclient import sample_tools
from oauth2client import client
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
# 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
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('account_id', None,
'The ID of the account with the specified ad unit',
short_name='a')
gflags.MarkFlagAsRequired('account_id')
gflags.DEFINE_string('ad_client_id', None,
'The ID of the ad client with the specified ad unit',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
gflags.DEFINE_string('ad_unit_id', None,
'The ID of the ad unit for which to get custom channels',
short_name='u')
gflags.MarkFlagAsRequired('ad_unit_id')
def main(argv):
# Process flags and read their values.
sample_utils.process_flags(argv)
account_id = gflags.FLAGS.account_id
ad_client_id = gflags.FLAGS.ad_client_id
ad_unit_id = gflags.FLAGS.ad_unit_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -85,7 +84,8 @@ def main(argv):
request = service.customchannels().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -23,17 +23,18 @@ Tags: savedadstyles.list
__author__ = 'jalc@google.com (Jose Alcerreca)'
import sys
from oauth2client.client import AccessTokenRefreshError
import sample_utils
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
def main(argv):
sample_utils.process_flags(argv)
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -55,7 +56,7 @@ def main(argv):
request = service.savedadstyles().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -23,17 +23,18 @@ Tags: savedreports.list
__author__ = 'jalc@google.com (Jose Alcerreca)'
import sys
from oauth2client.client import AccessTokenRefreshError
import sample_utils
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
def main(argv):
sample_utils.process_flags(argv)
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -48,7 +49,7 @@ def main(argv):
request = service.reports().saved().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -23,27 +23,27 @@ Tags: urlchannels.list
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import argparse
import sys
import gflags
from oauth2client.client import AccessTokenRefreshError
import sample_utils
from apiclient import sample_tools
from oauth2client import client
MAX_PAGE_SIZE = 50
# Declare command-line flags, and set them as required.
gflags.DEFINE_string('ad_client_id', None,
'The ad client ID for which to get URL channels',
short_name='c')
gflags.MarkFlagAsRequired('ad_client_id')
# 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):
# Process flags and read their values.
sample_utils.process_flags(argv)
ad_client_id = gflags.FLAGS.ad_client_id
# Authenticate and construct service.
service = sample_utils.initialize_service()
service, flags = sample_tools.init(
argv, 'adsense', 'v1.2', __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.
@@ -61,7 +61,7 @@ def main(argv):
request = service.customchannels().list_next(request, result)
except AccessTokenRefreshError:
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')

View File

@@ -1,118 +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.
"""Auxiliary file for AdSense Management API code samples.
Handles various tasks to do with logging, authentication and initialization.
"""
__author__ = 'sergio.gomes@google.com (Sergio Gomes)'
import logging
import os
import sys
from apiclient.discovery import build
import gflags
import httplib2
from oauth2client.client import flow_from_clientsecrets
from oauth2client.client import OOB_CALLBACK_URN
from oauth2client.file import Storage
from oauth2client.tools import run
FLAGS = gflags.FLAGS
# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
# application, including client_id and client_secret, which are found
# on the API Access tab on the Google APIs
# Console <http://code.google.com/apis/console>
CLIENT_SECRETS = 'client_secrets.json'
# Helpful message to display in the browser if the CLIENT_SECRETS file
# is missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the APIs Console <https://code.google.com/apis/console>.
""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)
# Set up a Flow object to be used if we need to authenticate.
FLOW = flow_from_clientsecrets(CLIENT_SECRETS,
scope='https://www.googleapis.com/auth/adsense.readonly',
redirect_uri=OOB_CALLBACK_URN,
message=MISSING_CLIENT_SECRETS_MESSAGE)
# The gflags module makes defining command-line options easy for applications.
# Run this program with the '--help' argument to see all the flags that it
# understands.
gflags.DEFINE_enum('logging_level', 'ERROR',
['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
'Set the level of logging detail.')
def process_flags(argv):
"""Uses the command-line flags to set the logging level."""
# Let the gflags module process the command-line arguments.
try:
argv = FLAGS(argv)
except gflags.FlagsError, e:
print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS)
sys.exit(1)
# Set the logging according to the command-line flag.
logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
def prepare_credentials():
"""Handles auth. Reuses credentialss if available or runs the auth flow."""
# If the credentials don't exist or are invalid run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('adsense.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run(FLOW, storage)
return credentials
def retrieve_service(http):
"""Retrieves an AdSense Management API service via the discovery service."""
# Construct a service object via the discovery service.
service = build("adsense", "v1.2", http=http)
return service
def initialize_service():
"""Builds instance of service from discovery data and does auth."""
# Create an httplib2.Http object to handle our HTTP requests.
http = httplib2.Http()
# Prepare credentials, and authorize HTTP object with them.
credentials = prepare_credentials()
http = credentials.authorize(http)
# Retrieve service.
return retrieve_service(http)