【问题标题】:How to read JSON data return by the Facebook access_token如何读取 Facebook access_token 返回的 JSON 数据
【发布时间】:2012-05-23 21:54:22
【问题描述】:

我正在尝试获取我网站中已登录用户的 Facebook 用户名。我的视图中有这段代码(在 Django 上):

code = request.GET.get('code')
url = 'https://graph.facebook.com/oauth/access_token?client_id=%(id)s&redirect_uri=http://127.0.0.1:8000/skempi/home&client_secret=%(secret)s&code=%(code)s'%{'id':fb_id,'secret':fb_s,'code':code}
response = urllib2.urlopen(url)
html = response.read()
dic = dict(urlparse.parse_qsl(html))
graph_url = 'https://graph.facebook.com/me?access_token='+dic.get('access_token')

当我执行return HttpResponseRedirect(graph_url) 时,我可以看到 JSON 数据。但是,我无法使用

读取该数据
import simplejson
d = simplejson.load(graph_url)
context ={'user':d}
return render_to_response('home.html', context, context_instance=RequestContext(request))    

我收到此错误:

'str' object has no attribute 'read'

【问题讨论】:

    标签: django json facebook-graph-api django-views


    【解决方案1】:

    您必须先下载 JSON,simplejson 才能对其进行解码。

    response = urllib2.urlopen(graph_url)
    data = simplejson.load(response)
    

    【讨论】:

      猜你喜欢
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多