OLD | NEW |
(Empty) | |
| 1 # Copyright (C) 2013 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 """This module holds the old run() function which is deprecated, the |
| 16 tools.run_flow() function should be used in its place.""" |
| 17 |
| 18 |
| 19 import logging |
| 20 import socket |
| 21 import sys |
| 22 import webbrowser |
| 23 |
| 24 import gflags |
| 25 |
| 26 from oauth2client import client |
| 27 from oauth2client import util |
| 28 from tools import ClientRedirectHandler |
| 29 from tools import ClientRedirectServer |
| 30 |
| 31 |
| 32 FLAGS = gflags.FLAGS |
| 33 |
| 34 gflags.DEFINE_boolean('auth_local_webserver', True, |
| 35 ('Run a local web server to handle redirects during ' |
| 36 'OAuth authorization.')) |
| 37 |
| 38 gflags.DEFINE_string('auth_host_name', 'localhost', |
| 39 ('Host name to use when running a local web server to ' |
| 40 'handle redirects during OAuth authorization.')) |
| 41 |
| 42 gflags.DEFINE_multi_int('auth_host_port', [8080, 8090], |
| 43 ('Port to use when running a local web server to ' |
| 44 'handle redirects during OAuth authorization.')) |
| 45 |
| 46 |
| 47 @util.positional(2) |
| 48 def run(flow, storage, http=None): |
| 49 """Core code for a command-line application. |
| 50 |
| 51 The run() function is called from your application and runs through all |
| 52 the steps to obtain credentials. It takes a Flow argument and attempts to |
| 53 open an authorization server page in the user's default web browser. The |
| 54 server asks the user to grant your application access to the user's data. |
| 55 If the user grants access, the run() function returns new credentials. The |
| 56 new credentials are also stored in the Storage argument, which updates the |
| 57 file associated with the Storage object. |
| 58 |
| 59 It presumes it is run from a command-line application and supports the |
| 60 following flags: |
| 61 |
| 62 --auth_host_name: Host name to use when running a local web server |
| 63 to handle redirects during OAuth authorization. |
| 64 (default: 'localhost') |
| 65 |
| 66 --auth_host_port: Port to use when running a local web server to handle |
| 67 redirects during OAuth authorization.; |
| 68 repeat this option to specify a list of values |
| 69 (default: '[8080, 8090]') |
| 70 (an integer) |
| 71 |
| 72 --[no]auth_local_webserver: Run a local web server to handle redirects |
| 73 during OAuth authorization. |
| 74 (default: 'true') |
| 75 |
| 76 Since it uses flags make sure to initialize the gflags module before |
| 77 calling run(). |
| 78 |
| 79 Args: |
| 80 flow: Flow, an OAuth 2.0 Flow to step through. |
| 81 storage: Storage, a Storage to store the credential in. |
| 82 http: An instance of httplib2.Http.request |
| 83 or something that acts like it. |
| 84 |
| 85 Returns: |
| 86 Credentials, the obtained credential. |
| 87 """ |
| 88 logging.warning('This function, oauth2client.tools.run(), and the use of ' |
| 89 'the gflags library are deprecated and will be removed in a future ' |
| 90 'version of the library.') |
| 91 if FLAGS.auth_local_webserver: |
| 92 success = False |
| 93 port_number = 0 |
| 94 for port in FLAGS.auth_host_port: |
| 95 port_number = port |
| 96 try: |
| 97 httpd = ClientRedirectServer((FLAGS.auth_host_name, port), |
| 98 ClientRedirectHandler) |
| 99 except socket.error, e: |
| 100 pass |
| 101 else: |
| 102 success = True |
| 103 break |
| 104 FLAGS.auth_local_webserver = success |
| 105 if not success: |
| 106 print 'Failed to start a local webserver listening on either port 8080' |
| 107 print 'or port 9090. Please check your firewall settings and locally' |
| 108 print 'running programs that may be blocking or using those ports.' |
| 109 print |
| 110 print 'Falling back to --noauth_local_webserver and continuing with', |
| 111 print 'authorization.' |
| 112 print |
| 113 |
| 114 if FLAGS.auth_local_webserver: |
| 115 oauth_callback = 'http://%s:%s/' % (FLAGS.auth_host_name, port_number) |
| 116 else: |
| 117 oauth_callback = client.OOB_CALLBACK_URN |
| 118 flow.redirect_uri = oauth_callback |
| 119 authorize_url = flow.step1_get_authorize_url() |
| 120 |
| 121 if FLAGS.auth_local_webserver: |
| 122 webbrowser.open(authorize_url, new=1, autoraise=True) |
| 123 print 'Your browser has been opened to visit:' |
| 124 print |
| 125 print ' ' + authorize_url |
| 126 print |
| 127 print 'If your browser is on a different machine then exit and re-run' |
| 128 print 'this application with the command-line parameter ' |
| 129 print |
| 130 print ' --noauth_local_webserver' |
| 131 print |
| 132 else: |
| 133 print 'Go to the following link in your browser:' |
| 134 print |
| 135 print ' ' + authorize_url |
| 136 print |
| 137 |
| 138 code = None |
| 139 if FLAGS.auth_local_webserver: |
| 140 httpd.handle_request() |
| 141 if 'error' in httpd.query_params: |
| 142 sys.exit('Authentication request was rejected.') |
| 143 if 'code' in httpd.query_params: |
| 144 code = httpd.query_params['code'] |
| 145 else: |
| 146 print 'Failed to find "code" in the query parameters of the redirect.' |
| 147 sys.exit('Try running with --noauth_local_webserver.') |
| 148 else: |
| 149 code = raw_input('Enter verification code: ').strip() |
| 150 |
| 151 try: |
| 152 credential = flow.step2_exchange(code, http=http) |
| 153 except client.FlowExchangeError, e: |
| 154 sys.exit('Authentication has failed: %s' % e) |
| 155 |
| 156 storage.put(credential) |
| 157 credential.set_store(storage) |
| 158 print 'Authentication successful.' |
| 159 |
| 160 return credential |
OLD | NEW |