Fix bug 7. Also, clean up the app engine template to make the happy path happier.

This commit is contained in:
Joe Gregorio
2011-01-05 11:22:47 -05:00
parent 83a7f6ad0c
commit 48c1caaacd
2 changed files with 44 additions and 29 deletions

View File

@@ -124,6 +124,9 @@ class OAuthCredentials(Credentials):
connection_type=None):
"""Modify the request headers to add the appropriate
Authorization header."""
response_code = 302
http.follow_redirects = False
while response_code in [301, 302]:
req = oauth.Request.from_consumer_and_token(
self.consumer, self.token, http_method=method, http_url=uri)
req.sign_request(signer, self.consumer, self.token)
@@ -134,8 +137,12 @@ class OAuthCredentials(Credentials):
headers['user-agent'] = self.user_agent + ' ' + headers['user-agent']
else:
headers['user-agent'] = self.user_agent
return request_orig(uri, method, body, headers,
resp, content = request_orig(uri, method, body, headers,
redirections, connection_type)
response_code = resp.status
if response_code in [301, 302]:
uri = resp['location']
return resp, content
http.request = new_request
return http

View File

@@ -36,7 +36,11 @@ from google.appengine.ext.webapp import util
from google.appengine.ext.webapp.util import login_required
APP_ID = os.environ['APPLICATION_ID']
STEP2_URI = 'http://%s.appspot.com/auth_return' % APP_ID
if 'Development' in os.environ['SERVER_SOFTWARE']:
STEP2_URI = 'http://localhost:8080/auth_return'
else:
STEP2_URI = 'http://%s.appspot.com/auth_return' % APP_ID
class Credentials(db.Model):
@@ -49,17 +53,22 @@ class MainHandler(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
storage = StorageByKeyName(Credentials, user.user_id(), 'credentials')
credentials = storage.get()
http = httplib2.Http()
if credentials:
http = credentials.authorize(http)
service = build("buzz", "v1", http=http)
credentials = storage.get()
if credentials:
http = credentials.authorize(http)
service = build("buzz", "v1", http=http)
if not credentials:
return begin_oauth_flow(self, user, service)
followers = service.people().list(userId='@me', groupId='@followers').execute()
self.response.out.write('Hello, you have %s followers!' %
followers['totalResults'])
else:
def begin_oauth_flow(request_handler, user, service):
flow = FlowThreeLegged(service.auth_discovery(),
consumer_key='anonymous',
consumer_secret='anonymous',
@@ -70,8 +79,7 @@ class MainHandler(webapp.RequestHandler):
authorize_url = flow.step1_get_authorize_url(STEP2_URI)
memcache.set(user.user_id(), pickle.dumps(flow))
self.redirect(authorize_url)
request_handler.redirect(authorize_url)
class OAuthHandler(webapp.RequestHandler):