Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 1 | # Copyright 2014 Google Inc. All Rights Reserved. |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Utilities for making samples. |
| 16 | |
| 17 | Consolidates a lot of code commonly repeated in sample applications. |
| 18 | """ |
INADA Naoki | e4ea1a9 | 2015-03-04 03:45:42 +0900 | [diff] [blame] | 19 | from __future__ import absolute_import |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 20 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 21 | __author__ = "jcgregorio@google.com (Joe Gregorio)" |
| 22 | __all__ = ["init"] |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 23 | |
| 24 | |
| 25 | import argparse |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 26 | import os |
| 27 | |
| 28 | from googleapiclient import discovery |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 29 | from googleapiclient.http import build_http |
Helen Koike | de13e3b | 2018-04-26 16:05:16 -0300 | [diff] [blame] | 30 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 31 | |
| 32 | def init( |
| 33 | argv, name, version, doc, filename, scope=None, parents=[], discovery_filename=None |
| 34 | ): |
| 35 | """A common initialization routine for samples. |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 36 | |
| 37 | Many of the sample applications do the same initialization, which has now |
| 38 | been consolidated into this function. This function uses common idioms found |
| 39 | in almost all the samples, i.e. for an API with name 'apiname', the |
| 40 | credentials are stored in a file named apiname.dat, and the |
| 41 | client_secrets.json file is stored in the same directory as the application |
| 42 | main file. |
| 43 | |
| 44 | Args: |
| 45 | argv: list of string, the command-line parameters of the application. |
| 46 | name: string, name of the API. |
| 47 | version: string, version of the API. |
| 48 | doc: string, description of the application. Usually set to __doc__. |
| 49 | file: string, filename of the application. Usually set to __file__. |
| 50 | parents: list of argparse.ArgumentParser, additional command-line flags. |
| 51 | scope: string, The OAuth scope used. |
Craig Citro | 63caac0 | 2014-10-15 10:11:30 -0700 | [diff] [blame] | 52 | discovery_filename: string, name of local discovery file (JSON). Use when discovery doc not available via URL. |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 53 | |
| 54 | Returns: |
| 55 | A tuple of (service, flags), where service is the service object and flags |
| 56 | is the parsed command-line flags. |
| 57 | """ |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 58 | try: |
| 59 | from oauth2client import client |
| 60 | from oauth2client import file |
| 61 | from oauth2client import tools |
| 62 | except ImportError: |
| 63 | raise ImportError( |
| 64 | "googleapiclient.sample_tools requires oauth2client. Please install oauth2client and try again." |
| 65 | ) |
Chris McDonough | bbaad28 | 2018-07-17 13:32:46 -0400 | [diff] [blame] | 66 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 67 | if scope is None: |
| 68 | scope = "https://www.googleapis.com/auth/" + name |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 69 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 70 | # Parser command-line arguments. |
| 71 | parent_parsers = [tools.argparser] |
| 72 | parent_parsers.extend(parents) |
| 73 | parser = argparse.ArgumentParser( |
| 74 | description=doc, |
| 75 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 76 | parents=parent_parsers, |
| 77 | ) |
| 78 | flags = parser.parse_args(argv[1:]) |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 79 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 80 | # Name of a file containing the OAuth 2.0 information for this |
| 81 | # application, including client_id and client_secret, which are found |
| 82 | # on the API Access tab on the Google APIs |
| 83 | # Console <http://code.google.com/apis/console>. |
| 84 | client_secrets = os.path.join(os.path.dirname(filename), "client_secrets.json") |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 85 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 86 | # Set up a Flow object to be used if we need to authenticate. |
| 87 | flow = client.flow_from_clientsecrets( |
| 88 | client_secrets, scope=scope, message=tools.message_if_missing(client_secrets) |
| 89 | ) |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 90 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 91 | # Prepare credentials, and authorize HTTP object with them. |
| 92 | # If the credentials don't exist or are invalid run through the native client |
| 93 | # flow. The Storage object will ensure that if successful the good |
| 94 | # credentials will get written back to a file. |
| 95 | storage = file.Storage(name + ".dat") |
| 96 | credentials = storage.get() |
| 97 | if credentials is None or credentials.invalid: |
| 98 | credentials = tools.run_flow(flow, storage, flags) |
| 99 | http = credentials.authorize(http=build_http()) |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 100 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 101 | if discovery_filename is None: |
| 102 | # Construct a service object via the discovery service. |
| 103 | service = discovery.build(name, version, http=http) |
| 104 | else: |
| 105 | # Construct a service object using a local discovery document file. |
| 106 | with open(discovery_filename) as discovery_file: |
| 107 | service = discovery.build_from_document( |
| 108 | discovery_file.read(), base="https://www.googleapis.com/", http=http |
| 109 | ) |
| 110 | return (service, flags) |