| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 Google Inc. All rights reserved. | |
| 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 """Command-line tools for authenticating via OAuth 2.0 | |
| 16 | |
| 17 Do the OAuth 2.0 Web Server dance for a command line application. Stores the | |
| 18 generated credentials in a common file that is used by other example apps in | |
| 19 the same directory. | |
| 20 """ | |
| 21 | |
| 22 from __future__ import print_function | |
| 23 | |
| 24 __author__ = 'jcgregorio@google.com (Joe Gregorio)' | |
| 25 __all__ = ['argparser', 'run_flow', 'run', 'message_if_missing'] | |
| 26 | |
| 27 import logging | |
| 28 import socket | |
| 29 import sys | |
| 30 import webbrowser | |
| 31 | |
| 32 from six.moves import BaseHTTPServer | |
| 33 from six.moves import urllib | |
| 34 | |
| 35 from oauth2client import client | |
| 36 from oauth2client import util | |
| 37 | |
| 38 | |
| 39 _CLIENT_SECRETS_MESSAGE = """WARNING: Please configure OAuth 2.0 | |
| 40 | |
| 41 To make this sample run you will need to populate the client_secrets.json file | |
| 42 found at: | |
| 43 | |
| 44 %s | |
| 45 | |
| 46 with information from the APIs Console <https://code.google.com/apis/console>. | |
| 47 | |
| 48 """ | |
| 49 | |
| 50 def _CreateArgumentParser(): | |
| 51 try: | |
| 52 import argparse | |
| 53 except ImportError: | |
| 54 return None | |
| 55 parser = argparse.ArgumentParser(add_help=False) | |
| 56 parser.add_argument('--auth_host_name', default='localhost', | |
| 57 help='Hostname when running a local web server.') | |
| 58 parser.add_argument('--noauth_local_webserver', action='store_true', | |
| 59 default=False, help='Do not run a local web server.') | |
| 60 parser.add_argument('--auth_host_port', default=[8080, 8090], type=int, | |
| 61 nargs='*', help='Port web server should listen on.') | |
| 62 parser.add_argument('--logging_level', default='ERROR', | |
| 63 choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], | |
| 64 help='Set the logging level of detail.') | |
| 65 return parser | |
| 66 | |
| 67 # argparser is an ArgumentParser that contains command-line options expected | |
| 68 # by tools.run(). Pass it in as part of the 'parents' argument to your own | |
| 69 # ArgumentParser. | |
| 70 argparser = _CreateArgumentParser() | |
| 71 | |
| 72 | |
| 73 class ClientRedirectServer(BaseHTTPServer.HTTPServer): | |
| 74 """A server to handle OAuth 2.0 redirects back to localhost. | |
| 75 | |
| 76 Waits for a single request and parses the query parameters | |
| 77 into query_params and then stops serving. | |
| 78 """ | |
| 79 query_params = {} | |
| 80 | |
| 81 | |
| 82 class ClientRedirectHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
| 83 """A handler for OAuth 2.0 redirects back to localhost. | |
| 84 | |
| 85 Waits for a single request and parses the query parameters | |
| 86 into the servers query_params and then stops serving. | |
| 87 """ | |
| 88 | |
| 89 def do_GET(self): | |
| 90 """Handle a GET request. | |
| 91 | |
| 92 Parses the query parameters and prints a message | |
| 93 if the flow has completed. Note that we can't detect | |
| 94 if an error occurred. | |
| 95 """ | |
| 96 self.send_response(200) | |
| 97 self.send_header("Content-type", "text/html") | |
| 98 self.end_headers() | |
| 99 query = self.path.split('?', 1)[-1] | |
| 100 query = dict(urllib.parse.parse_qsl(query)) | |
| 101 self.server.query_params = query | |
| 102 self.wfile.write("<html><head><title>Authentication Status</title></head>") | |
| 103 self.wfile.write("<body><p>The authentication flow has completed.</p>") | |
| 104 self.wfile.write("</body></html>") | |
| 105 | |
| 106 def log_message(self, format, *args): | |
| 107 """Do not log messages to stdout while running as command line program.""" | |
| 108 | |
| 109 | |
| 110 @util.positional(3) | |
| 111 def run_flow(flow, storage, flags, http=None): | |
| 112 """Core code for a command-line application. | |
| 113 | |
| 114 The run() function is called from your application and runs through all the | |
| 115 steps to obtain credentials. It takes a Flow argument and attempts to open an | |
| 116 authorization server page in the user's default web browser. The server asks | |
| 117 the user to grant your application access to the user's data. If the user | |
| 118 grants access, the run() function returns new credentials. The new credentials | |
| 119 are also stored in the Storage argument, which updates the file associated | |
| 120 with the Storage object. | |
| 121 | |
| 122 It presumes it is run from a command-line application and supports the | |
| 123 following flags: | |
| 124 | |
| 125 --auth_host_name: Host name to use when running a local web server | |
| 126 to handle redirects during OAuth authorization. | |
| 127 (default: 'localhost') | |
| 128 | |
| 129 --auth_host_port: Port to use when running a local web server to handle | |
| 130 redirects during OAuth authorization.; | |
| 131 repeat this option to specify a list of values | |
| 132 (default: '[8080, 8090]') | |
| 133 (an integer) | |
| 134 | |
| 135 --[no]auth_local_webserver: Run a local web server to handle redirects | |
| 136 during OAuth authorization. | |
| 137 (default: 'true') | |
| 138 | |
| 139 The tools module defines an ArgumentParser the already contains the flag | |
| 140 definitions that run() requires. You can pass that ArgumentParser to your | |
| 141 ArgumentParser constructor: | |
| 142 | |
| 143 parser = argparse.ArgumentParser(description=__doc__, | |
| 144 formatter_class=argparse.RawDescriptionHelpFormatter, | |
| 145 parents=[tools.argparser]) | |
| 146 flags = parser.parse_args(argv) | |
| 147 | |
| 148 Args: | |
| 149 flow: Flow, an OAuth 2.0 Flow to step through. | |
| 150 storage: Storage, a Storage to store the credential in. | |
| 151 flags: argparse.ArgumentParser, the command-line flags. | |
| 152 http: An instance of httplib2.Http.request | |
| 153 or something that acts like it. | |
| 154 | |
| 155 Returns: | |
| 156 Credentials, the obtained credential. | |
| 157 """ | |
| 158 logging.getLogger().setLevel(getattr(logging, flags.logging_level)) | |
| 159 if not flags.noauth_local_webserver: | |
| 160 success = False | |
| 161 port_number = 0 | |
| 162 for port in flags.auth_host_port: | |
| 163 port_number = port | |
| 164 try: | |
| 165 httpd = ClientRedirectServer((flags.auth_host_name, port), | |
| 166 ClientRedirectHandler) | |
| 167 except socket.error: | |
| 168 pass | |
| 169 else: | |
| 170 success = True | |
| 171 break | |
| 172 flags.noauth_local_webserver = not success | |
| 173 if not success: | |
| 174 print('Failed to start a local webserver listening on either port 8080') | |
| 175 print('or port 9090. Please check your firewall settings and locally') | |
| 176 print('running programs that may be blocking or using those ports.') | |
| 177 print() | |
| 178 print('Falling back to --noauth_local_webserver and continuing with') | |
| 179 print('authorization.') | |
| 180 print() | |
| 181 | |
| 182 if not flags.noauth_local_webserver: | |
| 183 oauth_callback = 'http://%s:%s/' % (flags.auth_host_name, port_number) | |
| 184 else: | |
| 185 oauth_callback = client.OOB_CALLBACK_URN | |
| 186 flow.redirect_uri = oauth_callback | |
| 187 authorize_url = flow.step1_get_authorize_url() | |
| 188 | |
| 189 if not flags.noauth_local_webserver: | |
| 190 webbrowser.open(authorize_url, new=1, autoraise=True) | |
| 191 print('Your browser has been opened to visit:') | |
| 192 print() | |
| 193 print(' ' + authorize_url) | |
| 194 print() | |
| 195 print('If your browser is on a different machine then exit and re-run this') | |
| 196 print('application with the command-line parameter ') | |
| 197 print() | |
| 198 print(' --noauth_local_webserver') | |
| 199 print() | |
| 200 else: | |
| 201 print('Go to the following link in your browser:') | |
| 202 print() | |
| 203 print(' ' + authorize_url) | |
| 204 print() | |
| 205 | |
| 206 code = None | |
| 207 if not flags.noauth_local_webserver: | |
| 208 httpd.handle_request() | |
| 209 if 'error' in httpd.query_params: | |
| 210 sys.exit('Authentication request was rejected.') | |
| 211 if 'code' in httpd.query_params: | |
| 212 code = httpd.query_params['code'] | |
| 213 else: | |
| 214 print('Failed to find "code" in the query parameters of the redirect.') | |
| 215 sys.exit('Try running with --noauth_local_webserver.') | |
| 216 else: | |
| 217 code = raw_input('Enter verification code: ').strip() | |
| 218 | |
| 219 try: | |
| 220 credential = flow.step2_exchange(code, http=http) | |
| 221 except client.FlowExchangeError as e: | |
| 222 sys.exit('Authentication has failed: %s' % e) | |
| 223 | |
| 224 storage.put(credential) | |
| 225 credential.set_store(storage) | |
| 226 print('Authentication successful.') | |
| 227 | |
| 228 return credential | |
| 229 | |
| 230 | |
| 231 def message_if_missing(filename): | |
| 232 """Helpful message to display if the CLIENT_SECRETS file is missing.""" | |
| 233 | |
| 234 return _CLIENT_SECRETS_MESSAGE % filename | |
| 235 | |
| 236 try: | |
| 237 from oauth2client.old_run import run | |
| 238 from oauth2client.old_run import FLAGS | |
| 239 except ImportError: | |
| 240 def run(*args, **kwargs): | |
| 241 raise NotImplementedError( | |
| 242 'The gflags library must be installed to use tools.run(). ' | |
| 243 'Please install gflags or preferrably switch to using ' | |
| 244 'tools.run_flow().') | |
| OLD | NEW |