一尘不染

上载Base64图像Facebook Graph API

node.js

我正在尝试使用Node.js将base64图像上传到FaceBook页面。我已经设法使上传与所有多部分数据一起工作,等等,如果我从文件系统读取文件(例如,使用fs.readFileSync(’c:\
a.jpg’)

但是,如果我使用base64编码的图像并尝试上传它,则会出现以下错误: {"error":{"message":"(#1) An unknown error occurred","type":"OAuthException","code":1}}

我尝试过将其转换为二进制文件new Buffer(b64string, 'base64');并上传,但是没有运气。

我已经为此苦苦挣扎了3天,因此,我们将不胜感激。

编辑:如果有人还知道我如何将base64转换为二进制文件并成功上传,那对我也将起作用。

编辑:代码段

var postDetails = separator + newlineConstant + 'Content-Disposition: form-data;name="access_token"' + newlineConstant + newlineConstant + accessToken + newlineConstant + separator;

postDetails = postDetails + newlineConstant + 'Content-Disposition: form-data; name="message"' + newlineConstant + newlineConstant + message + newlineConstant;

//Add the Image information
var fileDetailsString = '';
var index = 0;
var multipartBody = new Buffer(0);
images.forEach(function (currentImage) {
    fileDetailsString = fileDetailsString + separator + newlineConstant + 'Content-Disposition: file; name="source"; filename="Image' + index + '"' + newlineConstant + 'Content-Type: image/jpeg' + newlineConstant + newlineConstant;
    index++;

    multipartBody = Buffer.concat([multipartBody, new Buffer(fileDetailsString), currentImage]); //This is what I would use if Bianry data was passed in

    currentImage = new Buffer (currentImage.toString('base64'), 'base64'); // The following lines are what I would use for base64 image being passed in (The appropriate lines would be enabled/disabled if I was using Binary/base64)
    multipartBody = Buffer.concat([multipartBody, new Buffer(fileDetailsString), currentImage]);
});

multipartBody = Buffer.concat([new Buffer(postDetails), multipartBody, new Buffer(footer)]);

阅读 257

收藏
2020-07-07

共1个答案

一尘不染

上面的代码对我来说不太奏效(缺少逗号,type:"POST",并且blob函数的数据URI报错。我得到以下代码在Firefox和Chrome中运行:

function PostImageToFacebook(authToken)
{
    var canvas = document.getElementById("c");
    var imageData  = canvas.toDataURL("image/png");
    try {
        blob = dataURItoBlob(imageData);
    }
    catch(e) {
        console.log(e);
    }
    var fd = new FormData();
    fd.append("access_token",authToken);
    fd.append("source", blob);
    fd.append("message","Photo Text");
    try {
        $.ajax({
            url:"https://graph.facebook.com/me/photos?access_token=" + authToken,
            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("Posted to facebook");
            }
        });
    }
    catch(e) {
        console.log(e);
    }
}

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' });
}

这是GitHub https://github.com/DanBrown180/html5-canvas-post-to-facebook-
base64上的代码

2020-07-07