【发布时间】:2016-12-07 22:20:13
【问题描述】:
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 在 Internet 上上传已经托管的图像,但是当我尝试将本地文件添加到请求中时它不起作用。
我使用过nodeJS Library => facebook-node-sdk 但没有运气。
使用facebook-node-sdk,它使用消息和其他请求更新了用户提要,但图像上传不起作用。它给了我这个错误:
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);
});
然后我尝试使用 https nodejs 请求,如下所示:
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 我尝试了许多解决方案,例如为nodeJS 使用request npm 包,但似乎没有什么可以从local file path 或base64Data 或Blob 对象上传图像。
我们将不胜感激。
谢谢!
【问题讨论】:
标签: node.js facebook facebook-graph-api reactjs electron