Joe Gregorio | ba10e56 | 2011-12-08 13:16:42 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2011 Google Inc. All Rights Reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """Auxiliary file for AdSense Management API code samples. |
| 18 | |
| 19 | Handles various tasks to do with logging, authentication and initialization. |
| 20 | """ |
| 21 | |
| 22 | __author__ = 'sergio.gomes@google.com (Sergio Gomes)' |
| 23 | |
| 24 | import logging |
| 25 | import os |
| 26 | import sys |
| 27 | from apiclient.discovery import build |
| 28 | import gflags |
| 29 | import httplib2 |
| 30 | from oauth2client.client import flow_from_clientsecrets |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame^] | 31 | from oauth2client.client import OOB_CALLBACK_URN |
Joe Gregorio | ba10e56 | 2011-12-08 13:16:42 -0500 | [diff] [blame] | 32 | from oauth2client.file import Storage |
| 33 | from oauth2client.tools import run |
| 34 | |
| 35 | |
| 36 | FLAGS = gflags.FLAGS |
| 37 | |
| 38 | # CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this |
| 39 | # application, including client_id and client_secret, which are found |
| 40 | # on the API Access tab on the Google APIs |
| 41 | # Console <http://code.google.com/apis/console> |
| 42 | CLIENT_SECRETS = 'client_secrets.json' |
| 43 | |
| 44 | # Helpful message to display in the browser if the CLIENT_SECRETS file |
| 45 | # is missing. |
| 46 | MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 47 | WARNING: Please configure OAuth 2.0 |
| 48 | |
| 49 | To make this sample run you will need to populate the client_secrets.json file |
| 50 | found at: |
| 51 | |
| 52 | %s |
| 53 | |
| 54 | with information from the APIs Console <https://code.google.com/apis/console>. |
| 55 | |
| 56 | """ % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS) |
| 57 | |
| 58 | # Set up a Flow object to be used if we need to authenticate. |
| 59 | FLOW = flow_from_clientsecrets(CLIENT_SECRETS, |
| 60 | scope='https://www.googleapis.com/auth/adsense.readonly', |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame^] | 61 | redirect_uri=OOB_CALLBACK_URN, |
Joe Gregorio | ba10e56 | 2011-12-08 13:16:42 -0500 | [diff] [blame] | 62 | message=MISSING_CLIENT_SECRETS_MESSAGE) |
| 63 | |
| 64 | # The gflags module makes defining command-line options easy for applications. |
| 65 | # Run this program with the '--help' argument to see all the flags that it |
| 66 | # understands. |
| 67 | gflags.DEFINE_enum('logging_level', 'ERROR', |
| 68 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 69 | 'Set the level of logging detail.') |
| 70 | |
| 71 | |
| 72 | def process_flags(argv): |
| 73 | """Uses the command-line flags to set the logging level.""" |
| 74 | |
| 75 | # Let the gflags module process the command-line arguments. |
| 76 | try: |
| 77 | argv = FLAGS(argv) |
| 78 | except gflags.FlagsError, e: |
Joe Gregorio | 579c8c9 | 2012-02-07 14:31:01 -0500 | [diff] [blame] | 79 | print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS) |
Joe Gregorio | ba10e56 | 2011-12-08 13:16:42 -0500 | [diff] [blame] | 80 | sys.exit(1) |
| 81 | |
| 82 | # Set the logging according to the command-line flag. |
| 83 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 84 | |
| 85 | |
| 86 | def prepare_credentials(): |
| 87 | """Handles auth. Reuses credentialss if available or runs the auth flow.""" |
| 88 | |
| 89 | # If the credentials don't exist or are invalid run through the native client |
| 90 | # flow. The Storage object will ensure that if successful the good |
| 91 | # Credentials will get written back to a file. |
| 92 | storage = Storage('adsense.dat') |
| 93 | credentials = storage.get() |
| 94 | if credentials is None or credentials.invalid: |
| 95 | credentials = run(FLOW, storage) |
| 96 | return credentials |
| 97 | |
| 98 | |
| 99 | def retrieve_service(http): |
| 100 | """Retrieves an AdSense Management API service via the discovery service.""" |
| 101 | |
| 102 | # Construct a service object via the discovery service. |
Joe Gregorio | 579c8c9 | 2012-02-07 14:31:01 -0500 | [diff] [blame] | 103 | service = build("adsense", "v1.1", http=http) |
Joe Gregorio | ba10e56 | 2011-12-08 13:16:42 -0500 | [diff] [blame] | 104 | return service |
| 105 | |
| 106 | |
| 107 | def initialize_service(): |
| 108 | """Builds instance of service from discovery data and does auth.""" |
| 109 | |
| 110 | # Create an httplib2.Http object to handle our HTTP requests. |
| 111 | http = httplib2.Http() |
| 112 | |
| 113 | # Prepare credentials, and authorize HTTP object with them. |
| 114 | credentials = prepare_credentials() |
| 115 | http = credentials.authorize(http) |
| 116 | |
| 117 | # Retrieve service. |
| 118 | return retrieve_service(http) |