blob: 89f95ffe98ad0dabc9cceaf5b587d260e55b543d [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
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040031from oauth2client.client import OOB_CALLBACK_URN
Joe Gregoriob071ca72012-03-29 17:01:32 -040032from oauth2client.file import Storage
33from oauth2client.tools import run
34
35
36FLAGS = 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>
42CLIENT_SECRETS = 'client_secrets.json'
43
44# Helpful message to display in the browser if the CLIENT_SECRETS file
45# is missing.
46MISSING_CLIENT_SECRETS_MESSAGE = """
47WARNING: Please configure OAuth 2.0
48
49To make this sample run you will need to populate the client_secrets.json file
50found at:
51
52 %s
53
54with 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.
59FLOW = flow_from_clientsecrets(
60 CLIENT_SECRETS,
61 scope='https://www.googleapis.com/auth/adexchange.buyer',
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040062 redirect_uri=OOB_CALLBACK_URN,
Joe Gregoriob071ca72012-03-29 17:01:32 -040063 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.
69gflags.DEFINE_enum('logging_level', 'ERROR',
70 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
71 'Set the level of logging detail.')
72
73
74def 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
88def 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