Application Stack : Facebook Photo Upload + Graph API v.2.8 + Electron + NodeJS + ReactJS
我正在尝试在我的应用程序中实现Facebook照片共享。由于reactJS的一些电子JS问题,我手动进行了Facebook登录。
所有其他Facebook端点的工作方式如下:/me/feed
,/me/post
等
但是/me/photos
没有按预期工作。
它使用http url在互联网上上传已托管的图像但是当我尝试将本地文件添加到请求中时它没有工作。
我使用了nodeJS
Library => facebook-node-sdk
但没有运气。
使用facebook-node-sdk
更新了用户Feed以及消息和其他请求,但图片上传无效。它给了我这个错误:
TypeError: self._form.on is not a function
我在这里找出了问题:Node Facebook Issue
我将此代码用于facebook-node-sdk
:
FB.setAccessToken(ACCESS_TOKEN);
domtoimage.toBlob(node, { height: node.scrollHeight }).then(function (imageData) {
FB.api('me/photos', 'post', {
access_token: ACCESS_TOKEN,
url: 'https://www.facebook.com/images/fb_icon_325x325.png', // This one works fine
// This one below shows error : 'TypeError: self._form.on is not a function'
url: fs.createReadStream(`${SHARE_IMAGE_PATH}shareFile.png`),
// I tried also with Blob Object as :
url: imageData,
caption: 'Share',
debug: 'all'
}, function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log('Post Id: ' + res.post_id);
});
}).catch(function (error) {
console.error('oops, something went wrong!', error);
});
domtoimage.toPng(node, { height: node.scrollHeight }).then(function (imageData) {
let photoBuffer = new Buffer(imageData.replace(/^data:image\/\w+;base64,/, ''), 'base64');
shareImagePathExists().then(exists => exists ? exists : createShareImagePath()).then(() => {
log.info('Saving Screenshot to ' + SHARE_IMAGE_PATH);
fs.writeFile(`${SHARE_IMAGE_PATH}shareFile.png`, photoBuffer);
}).then(() => {
let formData = {
access_token: accessToken,
url: 'https://www.facebook.com/images/fb_icon_325x325.png', // This one works fine
// This one below returns 'requires upload file error'
url: fs.createReadStream(`${SHARE_IMAGE_PATH}shareFile.png`),
caption: 'Share',
debug: 'all'
};
console.log(formData);
let postData = querystring.stringify(formData);
let post = {
host: 'graph.facebook.com',
path: '/me/photos',
method: 'POST',
headers:
{
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length,
'Accept': 'application/json'
}
};
let req = https.request(post, function (response) {
let result = '';
response.on('data', function (data) {
result = result + data;
});
response.on('end', function () {
if (response && response.statusMessage === 'OK') {
dispatch(facebookActivityShareComplete());
dialog.showMessageBox({
title: 'Share Success',
message: 'Activity Shared to Facebook.',
buttons: []
});
console.log('Response Success: ', response);
}
});
response.on('error', function (err) {
console.log('REQUEST ERROR: ' + err.message);
});
console.log('Response recieved: ', response);
});
dispatch(facebookActivitySharing());
req.write(postData);
req.end();
});
}).catch(function (error) {
console.error('oops, something went wrong!', error);
});
以上代码每次都会返回requires upload file error
。我正在使用facebook graph API v2.8
我尝试了很多解决方案,例如使用{j}包为nodeJS使用{但从request
或local file path
或base64Data
上传图片似乎没有任何效果对象
任何形式的帮助都将受到赞赏。
谢谢!