2012-11-05

Facebook JavaScript SDK - Graph API Feed

Graph API - feed (個人動態)

讀取 feed 佖須取得 read_stream 權限,若要新增資料則需有 publish_stream 權限。(可參考)

Read

feed 讀取得資料類型分為: status 、 video 、 link 、 photo 等類型。
status 主要資料:
id(留言編號) 、 from(發佈者) 、 to(給xx) 、 message(訊息內容) 、 type(類別) 、 created_time(建立時間) 、 updated_time(更新時間) 、 likes(按讚資料) 、 comments(評論資料) …等
graph data:

 // 朋友的留言
  {
         "id": "100000368822917_472346759454281",
         "from": {
            "name": "\u76e7\u570b\u8aa0",
            "id": "100000117200218"
         },
         "to": {
            "data": [
               {
                  "name": "Jheng Winrex",
                  "id": "100000368822917"
               }
            ]
         },
         "message": "\u653f~ \u751f\u65e5\u5feb\u6a02  ^^",
         "actions": [
            {
               "name": "Comment",
               "link": "https://www.facebook.com/100000368822917/posts/472346759454281"
            },
            {
               "name": "Like",
               "link": "https://www.facebook.com/100000368822917/posts/472346759454281"
            }
         ],
         "type": "status",
         "created_time": "2012-09-19T13:25:08+0000",
         "updated_time": "2012-09-19T13:25:08+0000",
         "comments": {
            "count": 0
         }
      },
      
      或
      // 自已的留言
       {
         "id": "100000368822917_492676890754601",
         "from": {
            "name": "Jheng Winrex",
            "id": "100000368822917"
         },
         "message": "test message",
         "actions": [
            {
               "name": "Comment",
               "link": "https://www.facebook.com/100000368822917/posts/492676890754601"
            },
            {
               "name": "Like",
               "link": "https://www.facebook.com/100000368822917/posts/492676890754601"
            }
         ],
         "privacy": {
            "description": "Public",
            "value": "EVERYONE"
         },
         "type": "status",
         "status_type": "mobile_status_update",
         "created_time": "2012-11-10T06:50:04+0000",
         "updated_time": "2012-11-10T06:50:04+0000",
         "comments": {
            "count": 0
         }
      },
video 主要資料:
id(留言編踸) 、 from(發佈者) 、 story(分享訊息) 、 picture(影片圖片) 、 link(影片連結) 、 source(影片來源) 、 name(影片名稱) 、 description(影片簡介) 、 caption(截取自) 、 privacy(隱私設定) 、 message(訊息內容) 、 type(類別) 、 status_type(分享類型) 、 created_time(建立時間) 、 updated_time(更新時間) 、 likes(按讚資料) 、 comments(評論資料)
graph data:
 // 影片分享
 {
         "id": "100000368822917_504370676246147",
         "from": {
            "name": "Jheng Winrex",
            "id": "100000368822917"
         },
         "story": "Jheng Winrex shared a link.",
         "picture": "https://fbexternal-a.akamaihd.net/safe_image.php?...\u00252FS0GsxJymwIU\u00252Fmqdefault.jpg",
         "link": "http://www.youtube.com/watch?v=S0GsxJymwIU&feature=share",
         "source": "http://www.youtube.com/v/S0GsxJymwIU?version=3&autohide=1&autoplay=1",
         "name": "\u300a\u9f8d\u4e4b\u8c37 - \u9ed1\u9f8d\u5d1b\u8d77\u300b\u9810\u544a\u7247 - \u904a\u6232\u57fa\u5730",
         "caption": "www.youtube.com",
         "icon": "https://s-static.ak.facebook.com/rsrc.php/v2/yj/r/v2OnaTyTQZE.gif",
         "actions": [
            {
               "name": "Comment",
               "link": "https://www.facebook.com/100000368822917/posts/504370676246147"
            },
            {
               "name": "Like",
               "link": "https://www.facebook.com/100000368822917/posts/504370676246147"
            }
         ],
         "privacy": {
            "description": "Public",
            "value": "EVERYONE"
         },
         "type": "video",
         "status_type": "shared_story",
         "application": {
            "name": "Share_bookmarklet",
            "id": "5085647995"
         },
         "created_time": "2012-07-28T16:11:51+0000",
         "updated_time": "2012-07-28T16:11:51+0000",
         "comments": {
            "count": 0
         }
      }, 

link 主要資料:
id(留言編踸) 、 from(發佈者) 、 story(分享訊息) 、 picture(連結圖片) 、 link(連結連結) 、 name(連結名稱) 、 description(連結簡介) 、 caption(截取自) 、 privacy(隱私設定) 、 message(訊息內容) 、 type(類別) 、 status_type(分享類型) 、 created_time(建立時間) 、 updated_time(更新時間) 、 likes(按讚資料) 、 comments(評論資料)
graph data:
 // 分享連結
  "id": "100000368822917_440302242670844",
         "from": {
            "name": "Jheng Winrex",
            "id": "100000368822917"
         },
         "story": "Jheng Winrex shared a link.",
         "picture": "https://fbexternal-a.akamaihd.net/safe_image.php?...\u00252Fs1975324.jpg",
         "link": "http://www.nownews.com/2012/06/27/340-2828706.htm",
         "name": "\u8ca1\u7522\u906d\u67e5\u5c01\u6b206\u5104\u50b5...",
         "caption": "www.nownews.com",
         "description": "\u3014\u5a1b\u6a02\u4e2d\u5fc3\uff0f\u7d9c\u5408...",
         "icon": "https://s-static.ak.facebook.com/rsrc.php/v2/yD/r/aS8ecmYRys0.gif",
         "actions": [
            {
               "name": "Comment",
               "link": "https://www.facebook.com/100000368822917/posts/440302242670844"
            },
            {
               "name": "Like",
               "link": "https://www.facebook.com/100000368822917/posts/440302242670844"
            }
         ],
         "privacy": {
            "description": "Public",
            "value": "EVERYONE"
         },
         "type": "link",
         "status_type": "shared_story",
         "application": {
            "name": "Share_bookmarklet",
            "id": "5085647995"
         },
         "created_time": "2012-06-27T14:42:02+0000",
         "updated_time": "2012-06-27T14:42:02+0000",
         "comments": {
            "count": 0
         }
      },

photo 主要資料:
id(留言編踸) 、 from(發佈者) 、 story(分享訊息) 、 picture(照片) 、 link(圖片連結) 、 name(照片名稱) 、 description(照片簡介) 、 caption(截取自) 、 privacy(隱私設定) 、 message(訊息內容) 、 type(類別) 、 status_type(分享類型) 、 created_time(建立時間) 、 updated_time(更新時間) 、 likes(按讚資料) 、 comments(評論資料)
graph data:
 // 分享照片
  {
         "id": "1760907284_405104869545547",
         "from": {
            "name": "Yu-Jheng Lin",
            "id": "1760907284"
         },
         "story": "Yu-Jheng Lin shared \u5c31\u8b93\u6211\u5011\u4e00\u8d77\u4f86\u5e6b\u4f60's photo.",
         "picture": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-snc7/418712_274254072676517_1231412878_s.jpg",
         "link": "https://www.facebook.com/photo.php?fbid=274254072676517&set=a.271390819629509.46503.271389586296299&type=1",
         "name": "Timeline Photos",
         "caption": "\u5e6b\u5fd9\u5713\u4e00\u4f4d\u8001\u5148\u751f8/8\u7bc0\u7684\u5922...",
         "properties": [
            {
               "name": "By",
               "text": "\u5c31\u8b93\u6211\u5011\u4e00\u8d77\u4f86\u5e6b\u4f60",
               "href": "https://www.facebook.com/wehelpuuu?ref=stream"
            }
         ],
         "icon": "https://s-static.ak.facebook.com/rsrc.php/v2/yD/r/aS8ecmYRys0.gif",
         "actions": [
            {
               "name": "Comment",
               "link": "https://www.facebook.com/1760907284/posts/405104869545547"
            },
            {
               "name": "Like",
               "link": "https://www.facebook.com/1760907284/posts/405104869545547"
            }
         ],
         "type": "photo",
         "status_type": "shared_story", // 或  "status_type": "tagged_in_photo",
         "object_id": "274254072676517",
         "application": {
            "name": "Links",
            "id": "2309869772"
         },
         "created_time": "2012-08-09T15:17:17+0000",
         "updated_time": "2012-08-09T15:17:17+0000",
         "shares": {
            "count": 14
         },
         "comments": {
            "count": 0
         }
      },

分頁:
previous (前一頁)、 next (下一頁)、 since (開始時間)、 until (結束時間)。
 // 分頁資料
  "paging": {
      "previous": "https://graph.facebook.com/100000368822917/feed?limit=50&access_token=...&since=1352530204&__previous=1",
      "next": "https://graph.facebook.com/100000368822917/feed?limit=50&access_token=...&until=1336732255"
   }

javascript 取得 feed data code:
 
 $("#getFeed").click(function(){
   // 取得登入狀態
        FB.getLoginStatus(function(response) {
         
       if (response.status != 'connected') { // 是否 "不是" 連線狀態
        loginNow();
        return;
       }
       
         FB.api('/me/feed', { limit : 25 } , function(response){ //{ limit : 10 }  取得資料筆數,未設定預設值為 25
          
           var html = readResponse(response); //處理取得的資料…
      ....
         
         });
         
      });
  });
 
| Graph data

Post

feed post 參數不一定要全部都給值,可省略,(可參考)

 
  function postPublicMessage(){
   /* 
    * pamer 
    * picture : 圖片連結
    * linke : 連結位址
    * name : linke 名稱
    * caption : 發佈來源
    * content :  分享 linke 內容
    * desciption : 分享 linke 的舠介
    * message : 訊息本文
    * source : 影意資料來源
    */
   
   var pamer = {message: "", picture:"", link:"", name:"", caption:"", description:"", source:""};
    pamer.message = "讀取 feed 佖須取得 read_stream 權限,若要新增資料則需有 publish_stream 權限。";
  pamer.picture = "http://l.yimg.com/f/i/tw/hp/mh/09purple.gif";
  pamer.link = "http://winrex-ecm2.blogspot.tw/2012/11/facebook-javascript-sdk-graph-api-feed.html";
  pamer.name = "Facebook JavaScript SDK - Graph API Feed";
  pamer.caption = "http://winrex-ecm2.blogspot.tw";
  pamer.description = "簡介 Yahoo 首頁 內容";
  /*pamer.source = "";*/
  
 FB.getLoginStatus(function(response) { // 取得登入狀態
  if (response.status != 'connected') {
   //處理登入錯誤
   return;
  }
  
  //FB API 處理發佈訊息
  FB.api('/me/feed', 'post', pamer, function(response) { //方法參數依序為  graph api URL、 傳輸方式、參數、callback
   ShowTaoble(response);// 處理 回傳資料,回傳資料為 post id (發佈訊息的 id)
  });
 });
  }
post likes (按讚):
 
function like(postId,message){
   FB.getLoginStatus(function(response) { // 取得登入狀態
  if (response.status != 'connected') {
   //處理登入錯誤
   return;
  }
  
  var parame = {message: ""}
  parame.message = message; // 可省略 "I Likes it!" 
  
  //FB API ('/POST_ID/likes')
  FB.api('/' + postId +'/likes', 'post', parame, function(response) { // HTTP type 'post'
   
  });
 });
  }
  
delete likes (取消按讚):
 
  function deletelike(postId){
   FB.getLoginStatus(function(response) { // 取得登入狀態
  if (response.status != 'connected') {
   //處理登入錯誤
   return;
  }
  
  //FB API ('/POST_ID/likes')
  FB.api('/' + postId +'/likes', 'delete',  function(response) { //HTTP type 'delete'
   alert("delete likes!")
  });
 });
  }
  
post comments (發佈評論):
 
  function comment(postId, message){
   FB.getLoginStatus(function(response) { // 取得登入狀態
  if (response.status != 'connected') {
   //處理登入錯誤
   return;
  }
  var parame = {message:""};
  parame.message = message;
  //FB API 處理發佈訊息
  FB.api('/' + postId +'/comments', 'post', parame, function(response) { // HTTP type 'post'。
   alert(response.id);
   commentId = response.id;
  });
 });
  }
  
delete post (刪除評論或訊息):
 
  function delPost(postID){
   FB.getLoginStatus(function(response) { // 取得登入狀態
  if (response.status != 'connected') {
   //處理登入錯誤
   return;
  }
  
  //FB API 處理發佈訊息
  FB.api('/' + postID , 'delete', function(response) {
   alert("delete it !!");
   commentId = response.id;
  });
 });
  }
  

測試範例

message :


參考資料: