Package oauth2client :: Module old_run
[hide private]
[frames] | no frames]

Source Code for Module oauth2client.old_run

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