【问题标题】:Facebook photo uplaod is not working with electron + nodeJSFacebook 照片上传不适用于电子 + nodeJS
【发布时间】: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 pathbase64DataBlob 对象上传图像。

我们将不胜感激。

谢谢!

【问题讨论】:

    标签: node.js facebook facebook-graph-api reactjs electron


    【解决方案1】:

    对于错误: Uncaught TypeError: self._form.on is not a function request.js:1134.

    看起来 Electron 浏览器不支持对 request 节点包的 .on('error') 调用。而且我们不能使用node-facebook-sdkhere,因为它在后面使用request来发送数据请求。所以,我尝试了一些其他的包并让它与needlehere一起工作。然后我做了一些更多的研究来做一个multipart/form 请求将图像上传到facebook。并最终这样做:

    domtoimage.toBlob(node, { height: node.scrollHeight }).then(function (imageData) {
          let form = new FormData();
          form.append('filename',  'source');
          form.append('file', imageData);
          form.append('caption', CAPTION);
          let request = new XMLHttpRequest();
          request.open('POST', 'https://graph.facebook.com/me/photos?access_token=' + ACCESS_TOKEN);
          request.send(form);
          request.onload = () => {
            if (request.status !== 200) {
              console.log('Response : ', request.responseText);
            }
          };
        }).catch(function (error) {
          log.error('oops, something went wrong!', error);
        });
    

    而且效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2020-01-03
      • 2011-05-29
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多