2013年6月18日 星期二

Facebook Graph API


Facebook 開放了一組 REST-based 的 API,稱為 Graph API,供外部系統整合。

為什麼叫 "Graph" 呢,官網說,Facebook 的核心是個 Social Graph:
    
    "At Facebook's core is the social graph; ... The Graph API presents a simple, consistent view of the Facebook social graph, uniformly representing objects in the graph (e.g., people, photos, events, and pages) and the connections between them (e.g., friend relationships, shared content, and photo tags)."

讓我想到很久以前看到的一張圖,


還有我自己的Social Graph。


Anyway,
要寫 Facebook 應用,需要參考 Graph API 的文件,
http://developers.facebook.com/docs/reference/api/
找到文章中的"Graph API Explorer" ,連結點進去,大概就知道怎麼回事了。

試著在 FQL 輸入"/{PROFILE_ID}/friends",如果要查自己的朋友,"PROFILE_ID" 就填 "me",如果要查別人的朋友清單,就填那個人的 "PROFILE_ID"。其實 "me" 也有 PROFILE_ID,只是說可以用 "me" 來簡化。記得「取得存取代碼」中,需勾選 user_about_me 的 permission,來允許 API Explorer 這個 AP 有權限可以讀朋友清單。

而下方回傳的文字,就是以 JSON 格式回傳的朋友清單。
如果想知道所有 Facebook Object 的 JSON 細部規格,可以看 Graph API Reference 文件。而 JSON 的解析,我習慣用 Google GSON 的 library 將 JSON String 轉 Object。


沒有留言: