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