Update adsense samples to use apiclient.sample_tools.
Reviewed in https://codereview.appspot.com/8249044/.
diff --git a/samples/adsense/generate_report.py b/samples/adsense/generate_report.py
index 7edf54a..2c2f8c9 100644
--- a/samples/adsense/generate_report.py
+++ b/samples/adsense/generate_report.py
@@ -23,28 +23,29 @@
__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 @@
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')
diff --git a/samples/adsense/generate_report_with_paging.py b/samples/adsense/generate_report_with_paging.py
index 8217dee..965b069 100644
--- a/samples/adsense/generate_report_with_paging.py
+++ b/samples/adsense/generate_report_with_paging.py
@@ -27,29 +27,29 @@
__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 @@
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')
diff --git a/samples/adsense/get_account_tree.py b/samples/adsense/get_account_tree.py
index 91b7dac..a976b66 100644
--- a/samples/adsense/get_account_tree.py
+++ b/samples/adsense/get_account_tree.py
@@ -23,25 +23,26 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_accounts.py b/samples/adsense/get_all_accounts.py
index 36ea5f8..d53a0c8 100644
--- a/samples/adsense/get_all_accounts.py
+++ b/samples/adsense/get_all_accounts.py
@@ -22,17 +22,18 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_ad_clients.py b/samples/adsense/get_all_ad_clients.py
index b38ab3e..19fc527 100644
--- a/samples/adsense/get_all_ad_clients.py
+++ b/samples/adsense/get_all_ad_clients.py
@@ -22,17 +22,18 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_ad_clients_for_account.py b/samples/adsense/get_all_ad_clients_for_account.py
index 49ab765..87f29eb 100644
--- a/samples/adsense/get_all_ad_clients_for_account.py
+++ b/samples/adsense/get_all_ad_clients_for_account.py
@@ -21,26 +21,27 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_ad_units.py b/samples/adsense/get_all_ad_units.py
index 3eb6b8a..28f7988 100644
--- a/samples/adsense/get_all_ad_units.py
+++ b/samples/adsense/get_all_ad_units.py
@@ -23,27 +23,27 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_ad_units_for_custom_channel.py b/samples/adsense/get_all_ad_units_for_custom_channel.py
index a4ac18f..02d1378 100644
--- a/samples/adsense/get_all_ad_units_for_custom_channel.py
+++ b/samples/adsense/get_all_ad_units_for_custom_channel.py
@@ -23,39 +23,34 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_custom_channels.py b/samples/adsense/get_all_custom_channels.py
index e43b5e8..e10a85f 100644
--- a/samples/adsense/get_all_custom_channels.py
+++ b/samples/adsense/get_all_custom_channels.py
@@ -23,27 +23,27 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_custom_channels_for_ad_unit.py b/samples/adsense/get_all_custom_channels_for_ad_unit.py
index b1dd1ef..677c293 100644
--- a/samples/adsense/get_all_custom_channels_for_ad_unit.py
+++ b/samples/adsense/get_all_custom_channels_for_ad_unit.py
@@ -24,39 +24,38 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_saved_ad_styles.py b/samples/adsense/get_all_saved_ad_styles.py
index 395e342..f1f9b0c 100644
--- a/samples/adsense/get_all_saved_ad_styles.py
+++ b/samples/adsense/get_all_saved_ad_styles.py
@@ -23,17 +23,18 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_saved_reports.py b/samples/adsense/get_all_saved_reports.py
index 93747f2..65ab28d 100644
--- a/samples/adsense/get_all_saved_reports.py
+++ b/samples/adsense/get_all_saved_reports.py
@@ -23,17 +23,18 @@
__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 @@
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')
diff --git a/samples/adsense/get_all_url_channels.py b/samples/adsense/get_all_url_channels.py
index cede09d..3a8150b 100644
--- a/samples/adsense/get_all_url_channels.py
+++ b/samples/adsense/get_all_url_channels.py
@@ -23,27 +23,27 @@
__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 @@
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')
diff --git a/samples/adsense/sample_utils.py b/samples/adsense/sample_utils.py
deleted file mode 100644
index 1a03399..0000000
--- a/samples/adsense/sample_utils.py
+++ /dev/null
@@ -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)