AuthSub问题

发布于 2024-10-12 09:17:44 字数 4863 浏览 2 评论 0原文

我一直在梳理谷歌的身份验证文档,并且在 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/[email protected]/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/[email protected]/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 收集的内容/retriving_gdata_feeds.html,这就是我需要做的。

I've been combing through google's authentication documentation, and am having trouble with Auth Sub.

The following code is main.py of the beginning of a Google AppEngine app.

#!/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/[email protected]/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()

I've made it as far as receiving a session token, but now I'm lost.

I get an error message back, saying:

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/[email protected]/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': ''}

I thought I got a request token, upgraded it to a session token, stored it to the client.token_store. What I gathered from http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html, that's all I need to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-10-19 09:17:44

我最近更新了这篇文章,以提供有关如何在 App Engine 上使用 GData 和 OAuth 的更好文档:
http://code.google.com/appengine/articles/python/retriving_gdata_feeds.html

您应该能够使用那里的示例代码,并将 DocClient 替换为 CalendarService。

I recently updated this article to provide better documentation on how to use GData and OAuth on App Engine:
http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html

You should be able to use the sample code there, and replace DocClient with CalendarService.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文