blob: fd04097acf125bf63e681708367f5780c535042b [file] [log] [blame]
Joe Gregoriob071ca72012-03-29 17:01:32 -04001#!/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
19Handles various tasks to do with logging, authentication and initialization.
20"""
21
22__author__ = 'david.t@google.com (David Torres)'
23
24import logging
25import os
26import sys
27from apiclient.discovery import build
28import gflags
29import httplib2
30from oauth2client.client import flow_from_clientsecrets
31from oauth2client.file import Storage
32from oauth2client.tools import run
33
34
35FLAGS = gflags.FLAGS
36
37# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
38# application, including client_id and client_secret, which are found
39# on the API Access tab on the Google APIs
40# Console <http://code.google.com/apis/console>
41CLIENT_SECRETS = 'client_secrets.json'
42
43# Helpful message to display in the browser if the CLIENT_SECRETS file
44# is missing.
45MISSING_CLIENT_SECRETS_MESSAGE = """
46WARNING: Please configure OAuth 2.0
47
48To make this sample run you will need to populate the client_secrets.json file
49found at:
50
51 %s
52
53with information from the APIs Console <https://code.google.com/apis/console>.
54
55""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)
56
57# Set up a Flow object to be used if we need to authenticate.
58FLOW = flow_from_clientsecrets(
59 CLIENT_SECRETS,
60 scope='https://www.googleapis.com/auth/adexchange.buyer',
61 message=MISSING_CLIENT_SECRETS_MESSAGE
62 )
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.
67gflags.DEFINE_enum('logging_level', 'ERROR',
68 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
69 'Set the level of logging detail.')
70
71
72def 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:
79 print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS)
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
86def initialize_service():
87 """Initializes and returns an instance of the Ad Exchange Buyer service.
88
89 Authorizes the user for use of the service and returns it backs.
90
91 Returns:
92 The authorized and initialized service.
93 """
94
95 # Create an httplib2.Http object to handle our HTTP requests.
96 http = httplib2.Http()
97
98 # Prepare credentials, and authorize HTTP object with them.
99 # If the credentials don't exist or are invalid run through the native client
100 # flow. The Storage object will ensure that if successful the good
101 # credentials will get written back to a file.
102 storage = Storage('adexchangebuyer.dat')
103 credentials = storage.get()
104 if credentials is None or credentials.invalid:
105 credentials = run(FLOW, storage)
106 http = credentials.authorize(http)
107
108 # Construct a service object via the discovery service.
109 service = build('adexchangebuyer', 'v1', http=http)
110 return service
111