cgi.parse_qsl导致GAE出现奇怪的错误

时间:2011-06-24 14:10:15

标签: python google-app-engine cgi

我希望使用GAE构建一个facebook应用程序。以下代码

    from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import webapp

from urllib import urlopen, quote
import cgi
from django.utils import simplejson as json

class fbcheck(webapp.RequestHandler):
    def get(self):
        appid = 'XXX'
        appsecret = 'XXXX'
        myurl = 'XXX'

        code = self.request.get("code")
        if not code :
            dialogurl = 'https://facebook.com/dialog/outh?client_id=%s&redirect_uri=%s'%(appid,quote(myurl))
            self.response.out.write('<script> top.location.href="%s"</script>'%(dialogurl,))
        tokenurl = 'https://graph.facebook.com/oauth/access_token?client_id=%s&redirect_uri=%s&client_secret=%s&code=%s'%(appid,quote(myurl),appsecret,code)
        response = cgi.parse_qsl(urlopen(tokenurl))
        graphurl = 'https://graph.facebook.com/me?access_token=%s'%(response['access_token'],)
        user = json.loads(urlopen(graphurl))
        self.response.out.write('Hi %s'(user.name,))
        #self.response.out.write('hi')

application = webapp.WSGIApplication([('/', fbcheck),], debug=True)
def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

它给我以下错误

    Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 700, in __call__
    handler.get(*groups)
  File "/base/data/home/apps/s~dotsnboxes/1.351361343827624192/game.py", line 22, in get
    response = cgi.parse_qsl(urlopen(tokenurl))
  File "/base/python_runtime/python_dist/lib/python2.5/cgi.py", line 213, in parse_qsl
    pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
AttributeError: addinfourl instance has no attribute 'split'

对此有何建议?

1 个答案:

答案 0 :(得分:2)

应为response = cgi.parse_qsl(urlopen(tokenurl).read()); urlopen返回可以读取的类addinfourl的实例,而不是URL的内容。