2011-03-28 156 views

回答

0

下面是一個例子代碼:

var imgURL = 'URL de la photo a uploader'; 

FB.api('/ALBUM_ID/photos', 'post', { 
    message: ' Ma photo', 
    url: imgURL, 
}, function (response) { 

}); 
3

正確的代碼是:

var params = {}; 
params.url = 'https://myserver.com/myimage.jpg'; 

FB.api('/me/photos', 'post', params, function(response) { 
    if (!response || response.error) { 
     //error 
    } else { 
     picid = response.id; 
    } 
}); 

記住,照片必須是在服務器上,所以你需要一個服務器腳本上傳。 「url」參數是您上傳的圖像文件的絕對URL。 更多信息:https://developers.facebook.com/docs/reference/api/user/(請參閱照片/創建)

請記住,根據Facebook條款,「消息」參數必須是100%用戶生成的。您也無法發佈到登錄用戶的朋友牆上,該功能已被棄用且不再有效。

+0

有沒有辦法只能使用JavaScript和本地圖像做.. – Brune 2013-05-07 09:29:21

+0

這可能幫助你(特別是正確答案):http://stackoverflow.com/questions/4999024/facebook-graph-api -upload-照片使用的JavaScript – luschn 2013-05-07 09:37:59

0

我希望這會有用。通過僅在JavaScript的幫助下將照片上傳到FB,您可以使用以下方法。這裏需要的是imageData(它是base64格式的圖像)和MIME類型。

try{ 
     blob = dataURItoBlob(imageData,mimeType); 
}catch(e){console.log(e);} 
var fd = new FormData(); 
fd.append("access_token",accessToken); 
fd.append("source", blob);fd.append("message","Kiss"); 
try{ 
    $.ajax({ 
     url:"https://graph.facebook.com/" + <<userID received on getting user details>> + "/photos?access_token=" + <<user accessToken>>, 
     type:"POST" 
     data:fd, 
     processData:false, 
     contentType:false, 
     cache:false, 
     success:function(data){ 
      console.log("success " + data); 
     }, 
     error:function(shr,status,data){ 
      console.log("error " + data + " Status " + shr.status); 
     }, 
     complete:function(){ 
      console.log("Ajax Complete"); 
     } 
    }); 

}catch(e){console.log(e);} 

function dataURItoBlob(dataURI,mime) { 
    // convert base64 to raw binary data held in a string 
    // doesn't handle URLEncoded DataURIs 

    var byteString = window.atob(dataURI); 

    // separate out the mime component 


    // write the bytes of the string to an ArrayBuffer 
    //var ab = new ArrayBuffer(byteString.length); 
    var ia = new Uint8Array(byteString.length); 
    for (var i = 0; i < byteString.length; i++) { 
     ia[i] = byteString.charCodeAt(i); 
    } 

    // write the ArrayBuffer to a blob, and you're done 
    var blob = new Blob([ia], { type: mime }); 

    return blob; 
}