0

从 AuthSub 转到 OAuth,我想我正在慢慢开始理解这个过程。这个虽然给我一个错误:

#!/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

import gdata.gauth

from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError

SCOPES = 'https://www.google.com/calendar/feeds/' # in this case just one, but for future reference, just concatenate different scopes with a space in between
CONSUMER_KEY = 'jmm-timeline.appspot.com'
CONSUMER_SECRET = 'consumer secret key, here'
SIG_METHOD = gdata.auth.OAuthSignatureMethod.RSA_SHA1
f = open('remotekey.pem')
RSA_KEY = f.read()
f.close()


# Incomplete bibliography
# http://www.youtube.com/watch?v=bfgO-LXGpTM
# http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html


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):
    def get(self):

        self.write_page_header()

        try:
            client = gdata.calendar.service.CalendarService(source='jmm-timeline-v1')
            run_on_appengine(client)
            client.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.RSA_SHA1, consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, rsa_key=RSA_KEY)
            req_token = client.FetchOAuthRequestToken(SCOPES)
            oauth_callback_url = 'http://jmm-timeline.appspot.com/handle_authorized_request_token'
            oauth_authorization_url = client.GenerateOAuthAuthorizationURL(callback_url=oauth_callback_url)
            self.response.out.write(template.render('templates/authorization_prompt.html', { 'authorization_url': oauth_authorization_url }))
        except CapabilityDisabledError, e:
            self.response.out.write(template.render('templates/content/maintenance.html'))

        self.write_page_footer()

class HandleAuthorizedRequestToken(BasePage):
    def get(self):
        self.write_page_header()

        client = gdata.calendar.service.CalendarService('jmm-timeline-2');
        run_on_appengine(client)


        self.write_page_footer()

def main():
    application = webapp.WSGIApplication([('/', MainHandler), ('/handle_authorized_request_token', HandleAuthorizedRequestToken)], debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()

似乎 gdata 正在尝试读取我的 RSA_KEY 字符串,并且我得到了一个堆栈跟踪:

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 52, in get
    req_token = client.FetchOAuthRequestToken(SCOPES)
  File "C:\Users\Joshua\appengineapps\jmm-timeline\gdata\service.py", line 415, in FetchOAuthRequestToken
    extra_parameters=extra_parameters)
  File "C:\Users\Joshua\appengineapps\jmm-timeline\gdata\auth.py", line 217, in GenerateOAuthRequestTokenUrl
    oauth_input_params.GetConsumer(), None)
  File "C:\Users\Joshua\appengineapps\jmm-timeline\gdata\oauth\__init__.py", line 171, in sign_request
    self.set_parameter('oauth_signature', self.build_signature(signature_method, consumer, token))
  File "C:\Users\Joshua\appengineapps\jmm-timeline\gdata\oauth\__init__.py", line 175, in build_signature
    return signature_method.build_signature(self, consumer, token)
  File "C:\Users\Joshua\appengineapps\jmm-timeline\gdata\oauth\rsa.py", line 55, in build_signature
    privatekey = keyfactory.parsePrivateKey(cert)
  File "C:\Users\Joshua\appengineapps\jmm-timeline\gdata\tlslite\utils\keyfactory.py", line 203, in parsePrivateKey
    return parseXMLKey(s, private=True)
  File "C:\Users\Joshua\appengineapps\jmm-timeline\gdata\tlslite\utils\keyfactory.py", line 79, in parseXMLKey
    key = Python_RSAKey.parseXML(s)
  File "C:\Users\Joshua\appengineapps\jmm-timeline\gdata\tlslite\utils\Python_RSAKey.py", line 137, in parseXML
    element = xmltools.parseAndStripWhitespace(s)
  File "C:\Users\Joshua\appengineapps\jmm-timeline\gdata\tlslite\utils\xmltools.py", line 30, in parseAndStripWhitespace
    raise SyntaxError(str(e))
SyntaxError: syntax error: line 1, column 0
4

1 回答 1

2

tlslite 有两种密钥格式:PEM 和 XML。它首先尝试 PEM(参见 parsePrivateKey),然后回退到 XML。显然,您的 PEM 文件中有错误,因此 PEM 解析失败(但应该成功)。解析 XML 显然必须失败。

我的猜测是您以某种方式弄乱了行尾,但 PEM 文件可能存在更根本的问题。

于 2011-01-17T23:01:05.040 回答