NodeJS + FB api:将图像发布到页面



我创建了一个脚本,将图像发布到我的Facebook页面。我遵循这个文档https://developers.facebook.com/docs/graph-api/guides/upload#upload-file-data,但我被困在最后一步…他们说我必须输入文件处理程序返回上一步,但当我这样做时,我收到一个错误100:无效参数…

这是我到目前为止的代码:

FB.setAccessToken(ACCESS_TOKEN);
var stats = fs.statSync('./generatedSocialPictures/ILiga_260520221800_facebookPicture.png');
var fileSizeInBytes = stats.size;
console.log("SIZE OF FILE : " + fileSizeInBytes);
// Step 1 : get the ID uplaod Session
var sessionId = await FB.api(
'/app/uploads',
'POST',
{
'file_length': fileSizeInBytes,
'file_name': 'ILiga_260520221800_facebookPicture.png',
'file_type': 'image/png',
'session_type': 'attachment'
},
function(response) {
if(response.error) {
console.log(response.error);
return
}
SessionID = response.id
// Step 2 : Upload the file/image
FB.api(
'/'+SessionID,
'POST',
{
filename: "ILiga_260520221800_facebookPicture.png",
contentType: "image/png",
value: fs.createReadStream("./generatedSocialPictures/ILiga_260520221800_facebookPicture.png"),
},
function(response) {
if(response.error) {
console.log(response.error);
return
}
// Step 4 : Publish with the file handler in attachment
FB.api(
'/BetAssistantOfficiel/feed',
'POST',
{ 
"image_hash": response.h,
"name":"Name for testing",
"message": "Testing with api" },
function (response) {
if (response.error) {
console.log('error occurred: ')
console.log(response.error)
return;
}
console.log('successfully posted to page!');
}
);
}
);

你知道我哪里做错了吗?

谢谢你!

阅读了几个小时后,我发现前面描述的过程被破坏了…

我找到了一个方法来工作:

var filename = generateFacebookPicture(match_informations);
// Step 1 : upload file to facebook page album
FB.api(`/{album_id}/photos`, 'POST', 
{
'source': fs.createReadStream("PATH_TO_THE_LOCAL_FILE"),
// 'url': 'URL_OF_THE_IMAGE,
'caption': "Ceci est un test de publication avec image via api",
'alt_text_custom': 'Ceci est une alt description',
//'tag': [{'x':, 'y', 'tag_uid, tag_text'}]
// 'published': false ==> IF YOU WANT TO PUBLISH LATER
// 'scheduled_publish_time' ==> DATE OF THE PUBLICATION
}, 
function (response) {
if (response.error) {
console.log('error occurred: ')
console.log(response.error)
} else {
console.log('local photo uploaded to page!');
// Step 2 : publish media
FB.api(
'/BetAssistantOfficiel/feed',
'POST',
{ 
"object_attachment": response.id,
"message": `TODO`,
"link": "IF YOU WANT TO",
"name": "Bet Assistant - Match XXXXX",
"description": "Description",
},
function (response) {
if (response.error) {
console.log('error occurred: ')
console.log(response.error)
} else {
console.log('successfully posted to page!');
}
}
);    
}
}
);

希望对大家有所帮助!

最新更新