我正在尝试使用facebook JS sdk在facebook上发布图片。
我要发布的图片是画布中的图片,我使用以下代码:
function publishFacebook(){
console.log('dentro publish');
var dataURL = canvas.toDataURL()
var onlyData = dataURL.substring(dataURL.indexOf(',')+1, dataURL.length);
var decoded = Base64Binary.decode(onlyData);
var imageIwillPost = getFormData2(decoded, "aaa", "bbb.png");
console.log(imageIwillPost);
FB.api('/me/photos', 'POST',
{'source': '{' + imageIwillPost + '}' ,
'message': 'Whatever message we decide'},
function(resp) {
console.log('into function');
if (resp && !resp.error) {
console.log('uploaded');
console.log(resp);
} else {
console.log('some error');
console.log(resp.error);}});}
function getFormData2(imageData, name, filename){
var boundary = 'AaB03x';
var formData = '';
var formData = 'Content-Type: multipart/form-data; boundary=' + boundary + '\r\n';
formData += '--' + boundary + '\r\n'
formData += 'Content-Disposition: file; name="' + name + '"; filename="' + filename + '"\r\n';
formData += 'Content-Type: ' + 'image/png' + '\r\n';
formData += 'Content-Transfer-Encoding: binary'+ '\r\n';
formData += '\r\n';
for ( var i = 0; i < imageData.length; ++i ){
formData += String.fromCharCode( imageData[ i ] & 0xff );}
formData += '\r\n';
formData += '--' + boundary + '--' + '\r\n';
return formData;}
然而它不起作用并返回如下内容: 对象{error_code:1,error_msg:“发生未知错误”} 但有时它只是返回我没有被授权......