javascript- facebook上传照片

时间:2015-04-09 17:48:59

标签: javascript facebook facebook-graph-api

我正在开展Chrome扩展项目,我正在尝试将照片上传到脸谱相册,但无论我尝试上传哪张图片,它都会返回以下回复。

 {
   "error": {
      "message": "This message contains content that has been blocked by our security systems.",
      "type": "OAuthException",
      "code": 368
   }
}

这是我的代码:

var canvas = document.createElement('CANVAS');
	var ctx = canvas.getContext('2d');
	var img = new Image;
	img.crossOrigin = 'Anonymous';
	img.onload = function(){
		canvas.height = img.height;
		canvas.width = img.width;
	  	ctx.drawImage(img,0,0);
	  	var dataURL = canvas.toDataURL( 'image/png');
		 blob = dataURItoBlob(dataURL);
	  	 goon(blob);
	  	 canvas = null; 
	};
	img.src = "image url";

function dataURItoBlob(dataURI) {
var byteString = atob(dataURI.split(',')[1]);
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], { type: 'image/png' });
}
              
                                      
 function goon(kkv){
	var formdata= new FormData();

formdata.append("access_token", "Acces token");
formdata.append("message", "test message");
formdata.append("source", kkv);
formdata.append("privacy",  "{'value':'EVERYONE'}");
try {
    $.ajax({
        url: 'https://graph.facebook.com/me/photos',
        type: "POST",
        data: formdata,
        processData: false,
        contentType: false,
        cache: false,
        error: function (shr, status, data) {
            console.log("error " + data + " Status " + shr.status);
        },
        success: function (msg) {
            //successful
        }
    });
} catch (e) {
    console.log(e);
}
 }

我使用facebook graph api v2.3来获取访问令牌(具有扩展权限)

我曾尝试使用url参数insted of source但它给了我相同的响应

formdata.append("url", 'image url');

如果没有访问令牌,有没有办法做到这一点,我想在我的电脑上使用它只是我的Facebook帐户。如果没有,我怎么能摆脱这个错误?

1 个答案:

答案 0 :(得分:0)

不,您需要一个访问令牌以编程方式发布到Facebook。我收到类似的错误,我的研究似乎表明它与您发布网址的频率有关。特别是,如果您正在使用和应用程序,并且网址的域名不在应用程序的域中。最佳答案是频率较低或使用Facebook的广告API,但您可能会遇到费用。