Clean up OAuth 2 support

This commit is contained in:
Joe Gregorio
2011-01-28 16:36:13 -05:00
parent fd19cd3800
commit 49e94d8225
5 changed files with 47 additions and 41 deletions

View File

@@ -205,7 +205,7 @@ class OAuth2Credentials(Credentials):
if ((self.token_expiry is not None) and (self.token_expiry <= datetime.datetime.now())): if ((self.token_expiry is not None) and (self.token_expiry <= datetime.datetime.now())):
logging.info("Refreshing because %s <= %s" %(self.token_expiry, datetime.datetime.now())) logging.info("Refreshing because %s <= %s" %(self.token_expiry, datetime.datetime.now()))
self._refresh(request_orig) self._refresh(request_orig)
headers['authorization'] = 'WRAP access_token=' + self.access_token headers['authorization'] = 'OAuth ' + self.access_token
if 'user-agent' in headers: if 'user-agent' in headers:
headers['user-agent'] = self.user_agent + ' ' + headers['user-agent'] headers['user-agent'] = self.user_agent + ' ' + headers['user-agent']
else: else:

View File

@@ -39,8 +39,9 @@ except ImportError:
# TODO(jcgregorio) # TODO(jcgregorio)
# - docs # - docs
# - error handling # - error handling
# - oob when implemented
HOST_NAME = 'localhost'
PORT_NUMBERS = [8080, 8090]
class ClientRedirectServer(BaseHTTPServer.HTTPServer): class ClientRedirectServer(BaseHTTPServer.HTTPServer):
"""A server to handle OAuth 2.0 redirects back to localhost. """A server to handle OAuth 2.0 redirects back to localhost.
@@ -87,48 +88,50 @@ def run(flow, filename):
parser = OptionParser() parser = OptionParser()
parser.add_option("-f", "--file", dest="filename", parser.add_option("-f", "--file", dest="filename",
default=filename, help="write credentials to FILE", metavar="FILE") default=filename, help="write credentials to FILE", metavar="FILE")
parser.add_option("-p", "--no_local_web_server", dest="localhost",
action="store_false", # The support for localhost is a work in progress and does not
default=True, # work at this point.
help="Do not run a web server on localhost to handle redirect URIs") #parser.add_option("-p", "--no_local_web_server", dest="localhost",
parser.add_option("-w", "--local_web_server", dest="localhost", # action="store_false",
action="store_true", # default=True,
default=True, # help="Do not run a web server on localhost to handle redirect URIs")
help="Run a web server on localhost to handle redirect URIs")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
host_name = 'localhost' #if options.localhost:
port_numbers = [8080, 8090] # server_class = BaseHTTPServer.HTTPServer
if options.localhost: # try:
server_class = BaseHTTPServer.HTTPServer # port_number = PORT_NUMBERS[0]
try: # httpd = server_class((HOST_NAME, port_number), ClientRedirectHandler)
port_number = port_numbers[0] # except socket.error:
httpd = server_class((host_name, port_number), ClientRedirectHandler) # port_number = PORT_NUMBERS[1]
except socket.error: # try:
port_number = port_numbers[1] # httpd = server_class((HOST_NAME, port_number), ClientRedirectHandler)
try: # except socket.error:
httpd = server_class((host_name, port_number), ClientRedirectHandler) # options.localhost = False
except socket.error: # redirect_uri = 'http://%s:%s/' % (HOST_NAME, port_number)
options.localhost = False #else:
# redirect_uri = 'oob'
authorize_url = flow.step1_get_authorize_url('http://%s:%s/' % (host_name, port_number)) redirect_uri = 'oob'
authorize_url = flow.step1_get_authorize_url(redirect_uri)
print 'Go to the following link in your browser:' print 'Go to the following link in your browser:'
print authorize_url print authorize_url
print print
if options.localhost: #if options.localhost:
httpd.handle_request() # httpd.handle_request()
if 'error' in httpd.query_params: # if 'error' in httpd.query_params:
sys.exit('Authentication request was rejected.') # sys.exit('Authentication request was rejected.')
if 'code' in httpd.query_params: # if 'code' in httpd.query_params:
code = httpd.query_params['code'] # code = httpd.query_params['code']
else: #else:
accepted = 'n' accepted = 'n'
while accepted.lower() == 'n': while accepted.lower() == 'n':
accepted = raw_input('Have you authorized me? (y/n) ') accepted = raw_input('Have you authorized me? (y/n) ')
code = raw_input('What is the verification code? ').strip() code = raw_input('What is the verification code? ').strip()
credentials = flow.step2_exchange(code) credentials = flow.step2_exchange(code)

View File

@@ -63,8 +63,11 @@ class MainHandler(webapp.RequestHandler):
})) }))
else: else:
flow = OAuth2WebServerFlow( flow = OAuth2WebServerFlow(
client_id='anonymous', # Visit https://code.google.com/apis/console to
client_secret='anonymous', # generate your client_id, client_secret and to
# register your redirect_uri.
client_id='<YOUR CLIENT ID HERE>',
client_secret='<YOUR CLIENT SECRET HERE>',
scope='https://www.googleapis.com/auth/buzz', scope='https://www.googleapis.com/auth/buzz',
user_agent='buzz-cmdline-sample/1.0', user_agent='buzz-cmdline-sample/1.0',
domain='anonymous', domain='anonymous',

View File

@@ -26,8 +26,8 @@ from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run from oauth2client.tools import run
flow = OAuth2WebServerFlow( flow = OAuth2WebServerFlow(
client_id='anonymous', client_id='433807057907.apps.googleusercontent.com',
client_secret='anonymous', client_secret='jigtZpMApkRxncxikFpR+SFg',
scope='https://www.googleapis.com/auth/buzz', scope='https://www.googleapis.com/auth/buzz',
user_agent='buzz-cmdline-sample/1.0', user_agent='buzz-cmdline-sample/1.0',
domain='anonymous', domain='anonymous',

View File

@@ -26,8 +26,8 @@ from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run from oauth2client.tools import run
flow = OAuth2WebServerFlow( flow = OAuth2WebServerFlow(
client_id='anonymous', client_id='433807057907.apps.googleusercontent.com',
client_secret='anonymous', client_secret='jigtZpMApkRxncxikFpR+SFg',
scope='https://www.googleapis.com/auth/moderator', scope='https://www.googleapis.com/auth/moderator',
user_agent='moderator-cmdline-sample/1.0', user_agent='moderator-cmdline-sample/1.0',
xoauth_displayname='Moderator Client Example App') xoauth_displayname='Moderator Client Example App')