gpt4 book ai didi

javascript - 上传 Base64 图片 Facebook Graph API

转载 作者:IT老高 更新时间:2023-10-28 22:06:24 43 4
gpt4 key购买 nike

我正在尝试使用 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)]);

最佳答案

我希望这会很有用。通过仅在 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;
}

//编辑 AJAX 语法

关于javascript - 上传 Base64 图片 Facebook Graph API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16214300/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com