當我們的應用程式或網站通過了Facebook的認證,
就可以呼叫Facebook的Graph API進行許多互動。
本篇文章,
將簡介Graph API的其中一種操作模式:讀取資料。

基本上Facebook的Graph API在讀取資料上的格式實在是,
非常簡潔。

https://graph.facebook.com/ID

那就是上面這行,
其中ID可以是指定很多型式的對象,
ID可以是使用者ID、也可以是使用者的代號。
例如我們可以試試
https://graph.facebook.com/markzuckerberg
就可以看到Facebook創辦者Mark Zuckerberg設定公開的基本資料。

{
"id": "68310606562",
"name": "Mark Zuckerberg",
"picture": "http://profile.ak.fbcdn.net/object3/1116/30/s68310606562_3401.jpg",
"link": "http://www.facebook.com/markzuckerberg",
"category": "Public_figures_other",
"username": "markzuckerberg",
"personal_interests": "openness, making things that help people connect and share what's important to them, revolutions, information flow, minimalism\n\n\n",
"fan_count": 567708
}

除了上述例子,
ID這個欄位,
還可以代表Pages、Events、Groups、Applications、Status Message、Photos、Photo Albums、Videos與Notes。
此外,我們還可以在ID這個欄位代入ME這個參數,
這代表Graph API將會回傳當下登入Facebook的使用者的相關資訊。
而所有查詢到的資料Facebook會以JSON的格式回傳給我們。

關於讀取資料的Graph API,
Facebook還提供了進階版用法,
但一樣簡潔。

https://graph.facebook.com/ID/CONNECTION_TYPE

那就是在ID這個變數後面,
可以再加上CONNECTION_TYPE。
所謂CONNECTION_TYPE跟ID一樣有多種意義。
例如:

https://graph.facebook.com/me/friends

Facebook將會回傳當下登入Facebook的使用者其朋友清單。
CONNECTION_TYPE還有以下參數可以代入:


Friends: https://graph.facebook.com/me/friends
News feed: https://graph.facebook.com/me/home
Profile feed (Wall): https://graph.facebook.com/me/feed
Likes: https://graph.facebook.com/me/likes
Movies: https://graph.facebook.com/me/movies
Books: https://graph.facebook.com/me/books
Notes: https://graph.facebook.com/me/notes
Photo Tags: https://graph.facebook.com/me/photos
Photo Albums: https://graph.facebook.com/me/albums
Videos: https://graph.facebook.com/me/videos
Events: https://graph.facebook.com/me/events
Groups: https://graph.facebook.com/me/groups
Profile Picture: http://graph.facebook.com/ddsakura/picture
the list of users attending specific event: https://graph.facebook.com/331218348435/attending.


有關Read Data的Graph API基本上就是以上介紹的兩種格式了,
不過由於ID與CONNECTION_TYPE可以代表的物件很多,
所以我們可以參考Facebook所提供的Graph API Reference
在這個說明文件中,
Facebook將會說明每個物件的屬性,Connection_Types與動作與所需的延伸權限。
關於動作與延伸權限,
柯南將在下一篇文章繼續說明。

PS:以上API,注意在使用的時候要帶入之前所介紹的OAuth 2.0的access token,才可以正確執行喔。


arrow
arrow
    全站熱搜

    賽拉維‧柯南 發表在 痞客邦 留言(1) 人氣()