gpt4 book ai didi

Javascript Facebook API POST 多部分/表单数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:44:19 25 4
gpt4 key购买 nike

最近几天我一直被这个问题难倒了。如果有人能指出我正确的方向,我将不胜感激!我一直在尝试弄清楚如何通过 facebooks graph api 发布图像。

我通过他们的图形 API 从 Facebook 下载了一张图像,该图像正确显示在 Canvas 元素中。我正在通过在其上绘制文本来修改此元素,然后想将其上传回 facebook。我被困在上传中。

以下是我查看过的有用链接,但我仍然卡住了:

Facebook Graph API - upload photo using JavaScript讨论使用 multipart/form-data 通过帖子请求上传到 facebook。

https://gist.github.com/andyburke/1498758用于创建多部分表单数据并向 Facebook 发送请求以发布图像的代码。

这是我用来尝试将图片发布到 facebook 的代码

if ( XMLHttpRequest.prototype.sendAsBinary === undefined ) {
XMLHttpRequest.prototype.sendAsBinary = function(string) {
var bytes = Array.prototype.map.call(string, function(c) {
return c.charCodeAt(0) & 0xff;
});
//this.send(new Uint8Array(bytes).buffer); //depreciated warning
this.send(new Uint8Array(bytes));
};
}

// This function takes an array of bytes that are the actual contents of the image file.
// In other words, if you were to look at the contents of imageData as characters, they'd
// look like the contents of a PNG or GIF or what have you. For instance, you might use
// pnglib.js to generate a PNG and then upload it to Facebook, all from the client.
//
// Arguments:
// authToken - the user's auth token, usually from something like authResponse.accessToken
// filename - the filename you'd like the uploaded file to have
// mimeType - the mime type of the file, eg: image/png
// imageData - an array of bytes containing the image file contents
// message - an optional message you'd like associated with the image

function PostImageToFacebook( authToken, filename, mimeType, imageData, message )
{
console.log("filename " + filename);
console.log("mimeType " + mimeType);
console.log("imageData " + imageData);
console.log("message " + message);

// this is the multipart/form-data boundary we'll use
var boundary = '----ThisIsTheBoundary1234567890';

// let's encode our image file, which is contained in the var
var formData = '--' + boundary + '\r\n'
formData += 'Content-Disposition: form-data; name="source"; filename="' + filename + '"\r\n';
formData += 'Content-Type: ' + mimeType + '\r\n\r\n';
for ( var i = 0; i < imageData.length; ++i )
{
formData += String.fromCharCode( imageData[ i ] & 0xff );
}
formData += '\r\n';
formData += '--' + boundary + '\r\n';
formData += 'Content-Disposition: form-data; name="message"\r\n\r\n';
formData += message + '\r\n'
formData += '--' + boundary + '--\r\n';

var xhr = new XMLHttpRequest();
xhr.open( 'POST', 'https://graph.facebook.com/me/photos?access_token=' + authToken, true );
xhr.onload = xhr.onerror = function() {
console.log( xhr.responseText );
};
xhr.setRequestHeader( "Content-Type", "multipart/form-data; boundary=" + boundary );
xhr.sendAsBinary( formData );
console.log(formData);
}

对 PostImageToFacebook 函数的调用导致以下错误:

{
"error": {
"type": "Exception",
"message": "Your photos couldn't be uploaded. Photos should be less than 4 MB and saved as JPG, PNG, GIF or TIFF files.",
"code": 1366046
}

我记录了我在下面粘贴的 formData 的输出:

------ThisIsTheBoundary1234567890
Content-Disposition: form-data; name="source"; filename="MemeImage.png"
Content-Type: image/png

------ThisIsTheBoundary1234567890
Content-Disposition: form-data; name="message"

Posting a meme image
------ThisIsTheBoundary1234567890--

最佳答案

尝试替换

for ( var i = 0; i < imageData.length; ++i )
{
formData += String.fromCharCode( imageData[ i ] & 0xff );
}

formData += imageData;

关于Javascript Facebook API POST 多部分/表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22801975/

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