Joe Gregorio | b071ca7 | 2012-03-29 17:01:32 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2012 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 Ad Exchange Buyer API code samples. |
| 18 | |
| 19 | Handles various tasks to do with logging, authentication and initialization. |
| 20 | """ |
| 21 | |
| 22 | __author__ = 'david.t@google.com (David Torres)' |
| 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 | b071ca7 | 2012-03-29 17:01:32 -0400 | [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( |
| 60 | CLIENT_SECRETS, |
| 61 | scope='https://www.googleapis.com/auth/adexchange.buyer', |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame^] | 62 | redirect_uri=OOB_CALLBACK_URN, |
Joe Gregorio | b071ca7 | 2012-03-29 17:01:32 -0400 | [diff] [blame] | 63 | message=MISSING_CLIENT_SECRETS_MESSAGE |
| 64 | ) |
| 65 | |
| 66 | # The gflags module makes defining command-line options easy for applications. |
| 67 | # Run this program with the '--help' argument to see all the flags that it |
| 68 | # understands. |
| 69 | gflags.DEFINE_enum('logging_level', 'ERROR', |
| 70 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 71 | 'Set the level of logging detail.') |
| 72 | |
| 73 | |
| 74 | def process_flags(argv): |
| 75 | """Uses the command-line flags to set the logging level.""" |
| 76 | |
| 77 | # Let the gflags module process the command-line arguments. |
| 78 | try: |
| 79 | argv = FLAGS(argv) |
| 80 | except gflags.FlagsError, e: |
| 81 | print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS) |
| 82 | sys.exit(1) |
| 83 | |
| 84 | # Set the logging according to the command-line flag. |
| 85 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 86 | |
| 87 | |
| 88 | def initialize_service(): |
| 89 | """Initializes and returns an instance of the Ad Exchange Buyer service. |
| 90 | |
| 91 | Authorizes the user for use of the service and returns it backs. |
| 92 | |
| 93 | Returns: |
| 94 | The authorized and initialized service. |
| 95 | """ |
| 96 | |
| 97 | # Create an httplib2.Http object to handle our HTTP requests. |
| 98 | http = httplib2.Http() |
| 99 | |
| 100 | # Prepare credentials, and authorize HTTP object with them. |
| 101 | # If the credentials don't exist or are invalid run through the native client |
| 102 | # flow. The Storage object will ensure that if successful the good |
| 103 | # credentials will get written back to a file. |
| 104 | storage = Storage('adexchangebuyer.dat') |
| 105 | credentials = storage.get() |
| 106 | if credentials is None or credentials.invalid: |
| 107 | credentials = run(FLOW, storage) |
| 108 | http = credentials.authorize(http) |
| 109 | |
| 110 | # Construct a service object via the discovery service. |
| 111 | service = build('adexchangebuyer', 'v1', http=http) |
| 112 | return service |