簡體   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