我一直在梳理谷歌的身份验证文档,但在使用 Auth Sub 时遇到了问题。
以下代码是 Google AppEngine 应用程序开头的 main.py。
#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from gdata.calendar import service
import gdata
from gdata.alt.appengine import run_on_appengine
from google.appengine.api import users
from google.appengine.ext import db
from gdata.auth import OAuthSignatureMethod, OAuthToken, OAuthInputParams
import urllib
import simplejson
# Incomplete bibliography
# http://www.youtube.com/watch?v=bfgO-LXGpTM
# http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html
test = None
class BasePage(webapp.RequestHandler):
title = "Joshua's Construction Zone"
def write_page_header(self):
self.response.out.write(template.render('templates/header.html', {'title': self.title}))
def write_page_footer(self):
self.response.out.write(template.render('templates/footer.html', {}))
class MainHandler(BasePage):
client = None
def get(self):
self.write_page_header()
client = gdata.calendar.service.CalendarService()
run_on_appengine(client)
next_url = 'http://localhost:8085/handle_request_token'
scope = 'http://www.google.com/calendar/feeds/'
self.response.out.write('<a href=\"' + str(client.GenerateAuthSubURL(next_url, (scope,) , secure=False, session=True)) + '\">' + "Aquire Security Token" + '</a>')
self.write_page_footer()
class RPC(webapp.RequestHandler):
def getUserCalendars(calendar_service):
calendars = calendar_service.getUserCalendars()
json_cals = []
for calendar in calendars:
json_cals.insert(calendar)
json_cals = simplejson.json_encode(json_cals)
return json_cals
def get(self):
self.response.out.write('test')
class HandleRequestToken(BasePage):
def get(self):
self.write_page_header()
token = self.request.get('token')
client = gdata.calendar.service.CalendarService();
run_on_appengine(client)
auth_sub_token = gdata.auth.extract_auth_sub_token_from_url(self.request.url, 'http://www.google.com/calendar/feeds/')
session_token = client.upgrade_to_session_token(auth_sub_token)
if not session_token:
self.response.out.write('no session token')
return
client.token_store.add_token(session_token)
response = client.Get('http://www.google.com/calendar/feeds/joshua.marshall.moore@gmail.com/private/full/', converter=str)
self.response.out.write(response)
if not session_token:
self.response.out.write('no session token')
return
else:
self.response.out.write('session token achieved')
client.token_store.add_token(session_token)
user = users.get_current_user()
if not user:
self.response.out.write('no user')
return
client.Get('http://www.google.com/calendar/feeds/' + user.email() + '/private/full', converter=str)
self.write_page_footer()
def main():
application = webapp.WSGIApplication([('/', MainHandler), ('/rpc', RPC), ('/handle_request_token', HandleRequestToken)], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
我已经收到了会话令牌,但现在我迷路了。
我收到一条错误消息,说:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 515, in __call__
handler.get(*groups)
File "C:\Users\Joshua\appengineapps\jmm-timeline\main.py", line 75, in get
response = client.Get('http://www.google.com/calendar/feeds/joshua.marshall.moore@gmail.com/private/full/', converter=str)
File "C:\Users\Joshua\appengineapps\website\gdata\service.py", line 1108, in Get
'reason': server_response.reason, 'body': result_body}
RequestError: {'status': 401, 'body': '<HTML>\n<HEAD>\n<TITLE>Authorization required</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Authorization required</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n', 'reason': ''}
我以为我得到了一个请求令牌,将其升级为会话令牌,将其存储到 client.token_store。我从http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html收集到的,这就是我需要做的。