gpt4 book ai didi

python - AuthSub 问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:40 26 4
gpt4 key购买 nike

我一直在梳理谷歌的身份验证文档,但在使用 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()

我已经成功收到 session token ,但现在我迷路了。

我收到一条错误消息,说:

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': ''}

我以为我得到了一个请求 token ,将其升级为 session token ,并将其存储到 client.token_store。我从 http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html 收集到的信息,这就是我需要做的。

最佳答案

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

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

关于python - AuthSub 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4713089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com