简体   繁体   中英

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. I have this code in my view (on 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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