2012-11-03

FaceBook - Graphic API 基礎

Graphic API

在 Facebook 中每一個對象(人物、照片、活動及頁面)都有一個唯一的 ID。你可以連結對象的屬性要 https://graph.facebook.com/ID 如 Facebook 平台的官方網頁的 ID 為 19292868552 ,所以你可以獲取對象 https://graph.facebook.com/19292868552 的:

{
   "name": "Facebook Developers",
   "is_published": true,
   "website": "http://developers.facebook.com",
   "username": "FacebookDevelopers",
   "company_overview": "Facebook Platform enables anyone ...\n\n",
   "about": "Build and distribute amazing social apps on Facebook. ...",
   "talking_about_count": 16821,
   "category": "Product/service",
   "id": "19292868552",
   "link": "https://www.facebook.com/FacebookDevelopers",
   "likes": 222477,
   "cover": {
      "cover_id": "10151124971508553",
      "source": "http://sphotos-f.ak.fbcdn.net/.../...png",
      "offset_y": 0
   }
}
另外,也可以使用用戶名稱作為 ID ( https://graph.facebook.com/platform ),所有回傳的資料都為 JSON。 在 Facebook 上的所有對象可以用相同的方式連結:

Facebook 社交網中所有的對象都有彼此連接的關係,把這些關係在我們 API 的連接。您可以檢查對象的 URL 結構 https://graph.facebook.com/ID/CONNECTION_TYPE 之間的連接。支緩使用者和網頁的連接包括:

選擇回傳資料

在預設情況下,可以對大多數對象的屬性進行查詢並反回。你可以選擇要反回的"fields"查詢參數的 fields 。
例如:URL 回傳 ID, name, picture
https://graph.facebook.com/bgolub?fields=id,name,picture
也可以查詢中使用 "ids" 查詢參數要求多個對象,例如: https://graph.facebook.com?ids=arjun,vernal 會回傳二個相同 id 的資料。 "ids" 查詢參數,也可以接受網址。例如:
https://graph.facebook.com/?ids=http://www.imdb.com/title/tt0117500/
此外, "me"是一個特殊的標識符號,它指的是當前使用者。所以 https://graph.facebook.com/me 可以回傳的當前用戶的個人資料。

圖片

可以呈現尚前的任何對象的個人資料照片。例如。這是你的大頭照片:


  • 人物:<img src="https://graph.facebook.com/{用戶名稱}/picture"/>
  • 活動:<img src="https://graph.facebook.com/331218348435/picture"/>
  • 群組:<img src="https://graph.facebook.com/69048030774/picture"/>
  • 頁面:<img src="https://graph.facebook.com/DoloresPark/picture"/>
  • 應用程式:<img src="https://graph.facebook.com/2318966938/picture"/>
  • 相簿:<img src="https://graph.facebook.com/platform/picture"/>
人物 活動 群組 頁面 應用程式 相簿
你可以指定想要的圖片尺寸參數為"type",參數值有 square (50x50)、 small (50 pixels 寬, 自動調整高度)、 normal (100 pixels 寬, 自動調整高度)、 large (約 200 pixels 寬, 自動調整高度):
https://graph.facebook.com/me/picture?type=large
square | small | normal | large
如果需要使用安全傳輸取得圖片,可以設置 return_ssl_resources=1 參數:
https://graph.facebook.com/me/picture?return_ssl_resources=1

分頁

使用查詢時,有幾個有用的參數,可以透過連結過濾資料:

更多選項可參考Pagination documentation

日期

所有的日期字串將以ISO-8601格式回傳。你可以指定一個"date_format"查詢參數修改日期格式。可接受的格式字串需PHP日期函數所接受的相同: https://graph.facebook.com/platform/feed?date_format=U 。

Introspection

Graph API 支援 introspection 的對象,從而可以看到所有連接對象,前提是不知道它的類型。
https://graph.facebook.com/331218348435?metadata=1

Real-Time updates

Realtime Updates

搜尋(Searching)

您可以使用 https://graph.facebook.com/search 搜尋在facebook中的所有公共對象。其格式為:

https://graph.facebook.com/search?q=QUERY&type=OBJECT_TYPE
支援搜尋對象的類型:

發佈

你可以向 Facebook graph 發出 HTTP POST 請求適當的URL(必須使用 access token 參數)。例如:你可以發布一個新的訊息 https://graph.facebook.com/arjun/feed :

curl -F 'access_token=...'
-F 'message=Hello, Arjun.
https://graph.facebook.com/arjun/feed
post 相對應值: https://developers.facebook.com/docs/reference/api/post/
你也可以發表留言或類似的對象,如 /comments or /likes connection by posting to https://graph.facebook.com/OBJECT_ID/comments and https://graph.facebook.com/OBJECT_ID/likes, respectively :
curl -F 'access_token=...'
https://graph.facebook.com/313449204401/likes
大多數寫入操作需要更多的權限。
支援以下類型的對象:
方法描述參數
/PROFILE_ID/feed Publish a new post on the given profile's feed/wall message, picture, link, name, caption, description, source, place, tags
/OBJECT_ID/comments Comment on the given object (if it has a /comments connection) message
/OBJECT_ID/likes Like the given object (if it has a /likes connection none
/PROFILE_ID/notes Publish a note on the given profile message, subject
/PROFILE_ID/links Publish a link on the given profile link, message, picture, name, caption, description
/PROFILE_ID/events 新增活動 name, start_time, end_time
/EVENT_ID/attending 回覆"出席"參加活動 none
/EVENT_ID/maybe 回覆"也許"參加活動 none
/EVENT_ID/declined 回覆"拒絕"參加活動 none
/PROFILE_ID/albums 新增相簿 name, message
/ALBUM_ID/photos 上傳照片到指定相簿 message, source, place(multipart/form-data)
PROFILE_ID/checkins Create a checkin at a location represented by a Page coordinates, place, message, tags

刪除

你可以在 graph 中刪除對象,即通過 HTTP DELETE 請求,

DELETE https://graph.facebook.com/ID?access_token = ... HTTP/1.1
為了支援用戶端不支援所有HTTP 方法(如JavaScript客戶端),你也可以發出一個 POST 請求額外加入 method=delete :
您可以刪除這條評論發出POST請求,https://graph.facebook.com/COMMENT_ID?method=delete。
您可以刪除一個像發出DELETE請求,/的OBJECT_ID /喜歡(因為喜歡沒有ID)。

結論:

簡單的說就是使用 URL 下參數取得資料。

參考資料: