本例中沒有使用Facebook API SDK,從底層展示了Facebook API的工作流程。
給出了一個這樣的例子:如何在服務端中利用Facebook API獲取到用戶的email和好友列表。
1 申請Facebook API Key。
詳細步驟,參考這篇文章:http://help.webscribble.com/display/jconnector/Getting+the+Facebook+Connect+API+Key
注意:“Connect URL”(也就是Redirect URL,Facebook登錄成功后跳轉到的路徑)一定要設置正確。
例如,我這里設置為http://127.0.0.1:8080/facebookapi/。Facebook登錄成功后會跳轉到這個地址,並在URL后附帶access_token。
2 Facebook登錄鏈接。
用下面的程序段描述登錄鏈接的構成:
APP_ID就是第一步中申請得到的Application ID。
urllib.urlencode()是進行URL編碼。
- def home(request):
- """Index page
- """
- APP_ID = '179745182062082'
- REDIRECT_URI = 'http://127.0.0.1:8080/facebookapi/'
- login_link = 'https://www.facebook.com/dialog/oauth?' + urllib.urlencode({'client_id':APP_ID, 'redirect_uri':REDIRECT_URI, 'response_type': 'token', 'scope':'email'})
- return HttpResponse('<a href="%s">Log in Facebook</a>' % login_link)
scope參數指定需要用戶授權的權限(查看完整的權限列表),多個權限直接用逗號分隔。在本例中我們只請求用戶授予可獲取email的權限。
上述代碼生成的鏈接為:https://www.facebook.com/dialog/oauth?scope=publish_stream&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2F&response_type=token&client_id=179745182062082
如下圖所示:
3 用戶登錄、授權。
這部分是用戶在點擊(或自動跳轉到)上述鏈接后進行的操作。
4 獲取access_token。
用戶登錄,授權完畢后,將自動跳轉到如下的地址:
http://127.0.0.1:8080/facebookapi/#access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe&expires_in=88953
其中access_token是存取令牌,有了該參數就可以使用Facebook API獲取各種信息。
由於參數部分位於錨點符#之后,所以在服務端無法獲取到該參數,這里我們需要采用一種變通的方法:
- def facebookapi(request):
- """Get user's email and friends list via Facebook API
- """
- if request.GET.get('access_token'):
- return HttpResponse('<html><head><script>location = "?"+location.hash.slice(1);</script></head></html>')
- else:
- return HttpResponse('access_token=%s' % request.GET.get('access_token'))
通過JS獲取錨點后的參數並跳轉到http://127.0.0.1:8080/facebookapi/?access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe&expires_in=88953
這樣在服務端就能獲取到access_token參數了。
5 獲取用戶email和好友列表。
下面的API將以JSON格式返回當前用戶的基本信息。
https://graph.facebook.com/me/?access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe
返回數據格式:
{"id":"100002677566512","name":"Peng Qi","first_name":"Peng","last_name":"Qi","link":"http:\/\/www.facebook.com\/profile.php?id=100002677566512","gender":"male","email":"redice\u0040163.com","timezone":8,"locale":"en_US","updated_time":"2011-07-15T14:07:34+0000"}
下面的API將以JSON格式返回當前用戶的好友列表。
https://graph.facebook.com/me/friends?access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe
返回數據格式(當前只有一個名為Kim Kimmy的好友):
{"data":[{"name":"Kim Kimmy","id":"100000805925544"}],"paging":{"next":"https:\/\/graph.facebook.com\/me\/friends?access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe&limit=5000&offset=5000"}}
查看更多API的說明:http://developers.facebook.com/docs/reference/api/
Official Guide:https://developers.facebook.com/docs/graph-api/using-graph-api
