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