2013-02-18 61 views
7

我試着發送批量請求的圖形API,並獲得錯誤的第二請求的響應:使用Facebook的批量要求的JavaScript API

"{ 
    "error": { 
     "message": "(#100) Missing message or attachment", 
     "type": "OAuthException", 
     "code": 100 
    } 
}" 

誰能告訴我,我究竟做錯了什麼?

這裏是我使用的代碼:

var opts = { 
       message : 'Some message', 
       name : 'Post Name', 
       link : 'url', 
       description : 'The post Description', 
       picture : 'url to image' 
      }; 

FB.api('/', 'POST', { 
     batch: [ 
       { method: 'GET', relative_url: 'me/friends'}, 
       { method: "POST",relative_url: "me/feed", body : opts } 
     ] 
     }, function (response) { 
       console.log(response); 
     }); 
+0

我認爲在使用/訪問FB Graph API的某些部分時,您需要以某種方式驗證您的請求。 OAuthException似乎表明授權錯誤。 – kjetilh 2013-02-18 10:37:38

+0

第一個請求沒有錯誤,第二個請求獲取錯誤..即使當我在單獨的API請求中執行時,它的作用是FB.api('/ me/feed','post',opts,function(響應) { 如果(響應|| response.error) { } 其他 { } !}); – 2013-02-18 10:45:20

+0

發現問題和解決方法:身體領域。這應該被格式化爲原始HTTP POST正文字符串,類似於URL查詢字符串https://github.com/jgorset/facepy/issues/55 – 2013-02-18 11:57:52

回答

12

像沙龍說,你需要把體內場的URL編碼方式。

你可以做到這一點簡單的使用jQuery,如:

var opts = { 
       message : 'Some message', 
       name : 'Post Name', 
       link : 'url', 
       description : 'The post Description', 
       picture : 'url to image' 
      }; 

FB.api('/', 'POST', { 
     batch: [ 
       { method: 'GET', relative_url: 'me/friends'}, 
       { method: "POST",relative_url: "me/feed", body : $.param(opts) } 
     ] 
     }, function (response) { 
       console.log(response); 
     }); 

作品好。