Я просматривал документацию по аутентификации Google, и у меня возникли проблемы с Auth Sub.
Следующий код является main.py начала приложения Google AppEngine.
#!/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,, это все, что мне нужно сделать.