简体   繁体   English

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

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

I'm trying to get the facebook username of a logged in user in my site. 我正在尝试获取我网站上已登录用户的Facebook用户名。 I have this code in my view (on Django) : 我在我的视图中(在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')

when I do return HttpResponseRedirect(graph_url) I can see the JSON data. 当我return HttpResponseRedirect(graph_url)我可以看到JSON数据。 However, I'm not able to read that data using 但是,我无法使用

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

I get this error : 我收到此错误:

'str' object has no attribute 'read'

You have to actually download the JSON before simplejson can decode it. 您必须先下载JSON,然后simplejson才能对其进行解码。

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

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

相关问题 在Django中从Facebook的服务器端身份验证捕获access_token - Capture access_token from facebook's server-side authentication in Django 使用Django包facepy创建facebook通知:[15](#15)必须使用app access_token调用此方法 - Create a facebook notification with Django package facepy : [15] (#15) This method must be called with an app access_token 通过 access_token 注册时存储额外数据,如 refresh_token - Store extra data like refresh_token when signing up via access_token django-socialregistration access_token错误 - django-socialregistration access_token error 当视图返回json数据时如何设置csrf令牌cookie? - How to set csrf token cookie when a view return a json data? 如何使用python读取Facebook图形JSON数据? - How to read facebook graph json data using python? Django Rest Framework API如何从请求中提取access_token? - Django Rest Framework API How to extract access_token from request? 如何检查Twitter OAuth access_token是否仍然有效? - How do I check if a Twitter OAuth access_token is still valid? 在python或django本身过期时自动生成access_token - Auto generate an access_token when expired in python or django itself social-auth-app-django:刷新 access_token - social-auth-app-django: Refresh access_token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM