| OLD | NEW |
| 1 # Copyright 2014 Google Inc. All rights reserved. | 1 # Copyright 2014 Google Inc. All rights reserved. |
| 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 """Command-line tools for authenticating via OAuth 2.0 | 15 """Command-line tools for authenticating via OAuth 2.0 |
| 16 | 16 |
| 17 Do the OAuth 2.0 Web Server dance for a command line application. Stores the | 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 | 18 generated credentials in a common file that is used by other example apps in |
| 19 the same directory. | 19 the same directory. |
| 20 """ | 20 """ |
| 21 | 21 |
| 22 from __future__ import print_function | 22 from __future__ import print_function |
| 23 | 23 |
| 24 import logging | 24 import logging |
| 25 import socket | 25 import socket |
| 26 import sys | 26 import sys |
| 27 | 27 |
| 28 from six.moves import BaseHTTPServer | 28 from six.moves import BaseHTTPServer |
| 29 from six.moves import http_client |
| 29 from six.moves import urllib | 30 from six.moves import urllib |
| 30 from six.moves import input | 31 from six.moves import input |
| 31 | 32 |
| 32 from oauth2client import client | 33 from oauth2client import client |
| 33 from oauth2client import util | 34 from oauth2client import util |
| 34 | 35 |
| 35 | 36 |
| 36 __author__ = 'jcgregorio@google.com (Joe Gregorio)' | 37 __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 37 __all__ = ['argparser', 'run_flow', 'message_if_missing'] | 38 __all__ = ['argparser', 'run_flow', 'message_if_missing'] |
| 38 | 39 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 into the servers query_params and then stops serving. | 89 into the servers query_params and then stops serving. |
| 89 """ | 90 """ |
| 90 | 91 |
| 91 def do_GET(self): | 92 def do_GET(self): |
| 92 """Handle a GET request. | 93 """Handle a GET request. |
| 93 | 94 |
| 94 Parses the query parameters and prints a message | 95 Parses the query parameters and prints a message |
| 95 if the flow has completed. Note that we can't detect | 96 if the flow has completed. Note that we can't detect |
| 96 if an error occurred. | 97 if an error occurred. |
| 97 """ | 98 """ |
| 98 self.send_response(200) | 99 self.send_response(http_client.OK) |
| 99 self.send_header("Content-type", "text/html") | 100 self.send_header("Content-type", "text/html") |
| 100 self.end_headers() | 101 self.end_headers() |
| 101 query = self.path.split('?', 1)[-1] | 102 query = self.path.split('?', 1)[-1] |
| 102 query = dict(urllib.parse.parse_qsl(query)) | 103 query = dict(urllib.parse.parse_qsl(query)) |
| 103 self.server.query_params = query | 104 self.server.query_params = query |
| 104 self.wfile.write( | 105 self.wfile.write( |
| 105 b"<html><head><title>Authentication Status</title></head>") | 106 b"<html><head><title>Authentication Status</title></head>") |
| 106 self.wfile.write( | 107 self.wfile.write( |
| 107 b"<body><p>The authentication flow has completed.</p>") | 108 b"<body><p>The authentication flow has completed.</p>") |
| 108 self.wfile.write(b"</body></html>") | 109 self.wfile.write(b"</body></html>") |
| 109 | 110 |
| 110 def log_message(self, format, *args): | 111 def log_message(self, format, *args): |
| 111 """Do not log messages to stdout while running as cmd. line program.""" | 112 """Do not log messages to stdout while running as cmd. line program.""" |
| 112 | 113 |
| 113 | 114 |
| 114 @util.positional(3) | 115 @util.positional(3) |
| 115 def run_flow(flow, storage, flags, http=None): | 116 def run_flow(flow, storage, flags=None, http=None): |
| 116 """Core code for a command-line application. | 117 """Core code for a command-line application. |
| 117 | 118 |
| 118 The ``run()`` function is called from your application and runs | 119 The ``run()`` function is called from your application and runs |
| 119 through all the steps to obtain credentials. It takes a ``Flow`` | 120 through all the steps to obtain credentials. It takes a ``Flow`` |
| 120 argument and attempts to open an authorization server page in the | 121 argument and attempts to open an authorization server page in the |
| 121 user's default web browser. The server asks the user to grant your | 122 user's default web browser. The server asks the user to grant your |
| 122 application access to the user's data. If the user grants access, | 123 application access to the user's data. If the user grants access, |
| 123 the ``run()`` function returns new credentials. The new credentials | 124 the ``run()`` function returns new credentials. The new credentials |
| 124 are also stored in the ``storage`` argument, which updates the file | 125 are also stored in the ``storage`` argument, which updates the file |
| 125 associated with the ``Storage`` object. | 126 associated with the ``Storage`` object. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 146 | 147 |
| 147 parser = argparse.ArgumentParser( | 148 parser = argparse.ArgumentParser( |
| 148 description=__doc__, | 149 description=__doc__, |
| 149 formatter_class=argparse.RawDescriptionHelpFormatter, | 150 formatter_class=argparse.RawDescriptionHelpFormatter, |
| 150 parents=[tools.argparser]) | 151 parents=[tools.argparser]) |
| 151 flags = parser.parse_args(argv) | 152 flags = parser.parse_args(argv) |
| 152 | 153 |
| 153 Args: | 154 Args: |
| 154 flow: Flow, an OAuth 2.0 Flow to step through. | 155 flow: Flow, an OAuth 2.0 Flow to step through. |
| 155 storage: Storage, a ``Storage`` to store the credential in. | 156 storage: Storage, a ``Storage`` to store the credential in. |
| 156 flags: ``argparse.Namespace``, The command-line flags. This is the | 157 flags: ``argparse.Namespace``, (Optional) The command-line flags. This |
| 157 object returned from calling ``parse_args()`` on | 158 is the object returned from calling ``parse_args()`` on |
| 158 ``argparse.ArgumentParser`` as described above. | 159 ``argparse.ArgumentParser`` as described above. Defaults |
| 160 to ``argparser.parse_args()``. |
| 159 http: An instance of ``httplib2.Http.request`` or something that | 161 http: An instance of ``httplib2.Http.request`` or something that |
| 160 acts like it. | 162 acts like it. |
| 161 | 163 |
| 162 Returns: | 164 Returns: |
| 163 Credentials, the obtained credential. | 165 Credentials, the obtained credential. |
| 164 """ | 166 """ |
| 167 if flags is None: |
| 168 flags = argparser.parse_args() |
| 165 logging.getLogger().setLevel(getattr(logging, flags.logging_level)) | 169 logging.getLogger().setLevel(getattr(logging, flags.logging_level)) |
| 166 if not flags.noauth_local_webserver: | 170 if not flags.noauth_local_webserver: |
| 167 success = False | 171 success = False |
| 168 port_number = 0 | 172 port_number = 0 |
| 169 for port in flags.auth_host_port: | 173 for port in flags.auth_host_port: |
| 170 port_number = port | 174 port_number = port |
| 171 try: | 175 try: |
| 172 httpd = ClientRedirectServer((flags.auth_host_name, port), | 176 httpd = ClientRedirectServer((flags.auth_host_name, port), |
| 173 ClientRedirectHandler) | 177 ClientRedirectHandler) |
| 174 except socket.error: | 178 except socket.error: |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 storage.put(credential) | 239 storage.put(credential) |
| 236 credential.set_store(storage) | 240 credential.set_store(storage) |
| 237 print('Authentication successful.') | 241 print('Authentication successful.') |
| 238 | 242 |
| 239 return credential | 243 return credential |
| 240 | 244 |
| 241 | 245 |
| 242 def message_if_missing(filename): | 246 def message_if_missing(filename): |
| 243 """Helpful message to display if the CLIENT_SECRETS file is missing.""" | 247 """Helpful message to display if the CLIENT_SECRETS file is missing.""" |
| 244 return _CLIENT_SECRETS_MESSAGE % filename | 248 return _CLIENT_SECRETS_MESSAGE % filename |
| OLD | NEW |