我正在尝试从我的服务器向我的Facebook页面发布自动帖子,我正在使用Nodejs和Facebook-node-sdk。
我的问题是我无法添加图片而且我不知道如何使用此软件包上传视频。
这是我的代码,用Image(来自网址)发布在我的页面文本上,但这不起作用我的页面上只显示了消息。
var FB = require('fb');
var request = require('request');
FB.setAccessToken('MY_PAGE_TOKEN');
var body = 'My first post using facebook-sdk';
FB.api('me/feed/', 'post', { message: body ,url : 'http://google.com/doodle.png'}, function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log('Post Id: ' + res.id);
});
答案 0 :(得分:0)
要上传您需要发布到/ photos端点的照片,而不是/ feed端点。
https://developers.facebook.com/docs/graph-api/reference/user/photos/#Creating
FB.api('me/photos/', 'post', { caption: 'Photo description' ,url : 'http://google.com/doodle.png'}, function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log('Post Id: ' + res.id);
});
答案 1 :(得分:0)
以下是我一年前使用Facebook Graph API在Node Js上传视频的代码:
// It publishes the video in chunks to Facebook until it finishes
var publishVideo = function(access_token, text, file) {
var stats = fs.statSync(file.path);
var formData = {
access_token: access_token,
upload_phase: 'start',
file_size: stats.size
};
// First step, we send the video size
request.post({ url: 'https://graph-video.facebook.com/v2.3/me/videos', form: formData, json: true },
function(err, response, body) {
if (!err) {
// With the response, we start making the video transfer
transferProcess(undefined, file, access_token, body, function(err, currentUploadSession) {
if (!err) {
var formData = {
access_token: access_token,
upload_phase: 'finish',
upload_session_id: currentUploadSession,
description: text
};
// Once the video transfer ended, we publish the video
request.post({ url: 'https://graph-video.facebook.com/v2.3/me/videos', form: formData, json: true });
}
});
}
});
};
// It processes each part of the video until it finishes
var transferProcess = function(uploadSession, file, access_token, body, callback) {
// First we generate a copy of the file in order to be independent to the original file
// because it can have problems when opening it at the same time from other file
var copyFileName = file.path + '-facebook';
fse.copySync(file.path, copyFileName);
// Once we have the copy, we open it
var fd = fs.openSync(copyFileName, 'r');
var bytesRead, data, bufferLength = 1000000000;
var buffer = new Buffer(bufferLength);
var length = body.end_offset - body.start_offset;
// We read the amount of bytes specified from body.start_offset until length
bytesRead = fs.readSync(fd, buffer, body.start_offset, length, null);
data = bytesRead < bufferLength ? buffer.slice(0, bytesRead) : buffer;
// We generate a file with the recently read data, and with a name of copyFileName-chunked-12313123
var chunkFileName = copyFileName + '-chunked-' + body.start_offset;
// We create the file so then we can read it and send it
fs.writeFile(chunkFileName, data, function(err) {
if (err) {
callback(err);
}
else {
var currentUploadSession = uploadSession ? uploadSession : body.upload_session_id;
var startOffset = parseInt(body.start_offset);
var formData = {
upload_phase: 'transfer',
start_offset: startOffset,
upload_session_id: currentUploadSession,
access_token: access_token
};
formData.video_file_chunk = fs.createReadStream(chunkFileName);
// Once we have the file written, we upload it
request.post({ url: 'https://graph-video.facebook.com/v2.3/me/videos',
formData: formData, json: true }, function (err, response, body) {
// If there was an error, we return it
if (err || body.error) {
callback(err ? err : body.error, null);
}
// If the lecture of the file has ended, facebook will send us the body.start_offset and the body.end_offset,
// if they are the same, it means that we have ended uploading the video, so we return
else if (body.start_offset === body.end_offset) {
callback(err, currentUploadSession);
}
// Else, we keep reading the file
else {
transferProcess(currentUploadSession, file, access_token, body, callback);
}
});
}
});
};
希望你能用它。
乔尔