繁体   English   中英

如何读取Facebook access_token返回的JSON数据

[英]How to read JSON data return by the Facebook access_token

我正在尝试获取我网站上已登录用户的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'

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM