Returning the number of likes of a facebook page using Graph API in Python to my .html page
我必须在Django网站的主页上显示我的Facebook页面的关注者数量。
(我以前只内置了Rails,所以我是Python/Danjo的一个真正的笨蛋,我已经在这上面呆了两个星期了)。
我一直在使用这篇文章中的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import urllib2 import json def get_page_data(page_id,access_token): api_endpoint ="https://graph.facebook.com/v2.4/" fb_graph_url = api_endpoint+page_id+"?fields=id,name,likes,link&access_token="+access_token try: api_request = urllib2.Request(fb_graph_url) api_response = urllib2.urlopen(api_request) try: return json.loads(api_response.read()) except (ValueError, KeyError, TypeError): return"JSON error" except IOError, e: if hasattr(e, 'code'): return e.code elif hasattr(e, 'reason'): return e.reason page_id ="idiotinside" # username or id token ="TOKEN" # Access Token page_data = get_page_data(page_id,token) print"Page Name:"+ page_data['name'] print"Likes:"+ str(page_data['likes']) print"Link:"+ page_data['link'] |
它在控制台上打印的非常好,但是当我尝试将其返回到fb.html时,我得到:
IndentationError at /
unexpected indent (views.py, line 67)
当我尝试在相同的打印级别缩进"链接:"+page_data["链接"]时,我得到:
SyntaxError at /
'return' outside function (views.py, line 67)
如果我像这样缩进:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | def get_page_data(page_id,access_token): import ipdb; ipdb.set_trace() api_endpoint ="https://graph.facebook.com/v2.4/" fb_graph_url = api_endpoint+page_id+"?fields=id,name,likes,link&access_token="+access_token try: api_request = urllib2.Request(fb_graph_url) api_response = urllib2.urlopen(api_request) try: return json.loads(api_response.read()) except (ValueError, KeyError, TypeError): return"JSON error" except IOError, e: if hasattr(e, 'code'): return e.code elif hasattr(e, 'reason'): return e.reason import ipdb; ipdb.set_trace() page_id ="xxxxxxxxx" # username or id token ="xxxxx" # Access Token page_data = get_page_data(page_id,token) print"Page Name:"+ page_data['name'] print"Likes:"+ str(page_data['likes']) print"Link:"+ page_data['link'] return render_to_response(page_data['name'], str(page_data['likes']), page_data['link']) |
在def get页面数据下的ipdb甚至不会被触发。
我尝试通过使用Facebook SDK 0.4.0和FB 0.2.0来寻找另一种解决方案,但对于像我这样的初学者来说,它们的文档还不够清晰/教程化。
求求你,无论哪种帮助都会很感激…
我得到了一位高级开发人员的帮助,下面是解决方案:
首先设置URL:
1 | url(r'^(?P<page_name>fb)/$', 'pages.views.facebook_page', name='facebook-page'), |
然后在pages/views.py中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | def facebook_page(request, page_name): page_id =".............." # username or id token ="................" # Access Token context = get_page_data(page_id,token) return render_to_response('pages/fb.html', context, context_instance=RequestContext(request)) def get_page_data(page_id,access_token): api_endpoint ="https://graph.facebook.com/v2.4/" fb_graph_url = api_endpoint+page_id+"?fields=id,name,likes,link&access_token="+access_token try: api_request = urllib2.Request(fb_graph_url) api_response = urllib2.urlopen(api_request) try: return json.loads(api_response.read()) except (ValueError, KeyError, TypeError): return"JSON error" except IOError, e: if hasattr(e, 'code'): return e.code elif hasattr(e, 'reason'): return e.reason |
它可以在这样的视图中回忆:
1 2 | <li class="social">Likes: {{likes}} </li> |