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())):
logging.info("Refreshing because %s <= %s" %(self.token_expiry, datetime.datetime.now()))
self._refresh(request_orig)
headers['authorization'] = 'WRAP access_token=' + self.access_token
headers['authorization'] = 'OAuth ' + self.access_token
if 'user-agent' in headers:
headers['user-agent'] = self.user_agent + ' ' + headers['user-agent']
else:

View File

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

View File

@@ -63,8 +63,11 @@ class MainHandler(webapp.RequestHandler):
}))
else:
flow = OAuth2WebServerFlow(
client_id='anonymous',
client_secret='anonymous',
# Visit https://code.google.com/apis/console to
# 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',
user_agent='buzz-cmdline-sample/1.0',
domain='anonymous',

View File

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

View File

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