Node.js - Send file to Facebook Graph Api

2019-08-25 03:46发布

问题:

I need to upload images and other content to Facebook from a Node.js backend. My API only make requests using the Graph API endpoints, and I don't want to use unofficial Node.js SDKs.

I can send files from my front end and retrieve them using multer. However I don't want to save them to a directory, but send them directly to a Graph API endpoint like https://graph.facebook.com/<page-id>/photos?access_token=<token-id>

What should I do to achieve that?

Edit:

Here's what I have for the moment. Multer save the file as binary data in an uploads directory

router.post('/profile/:idProfile/post', upload.any(), (req, res) => {
  const path = req.params.idProfile + '/photos';

  console.log(req.files[0]);

  // A facebook superset module created for my app
  facebook.post(path, req.files[0])
    .then((res2) => {
      if (res2.error) {
        res.send(res2)
      } else {
        res.send({
          code: 200,
          data: req.files[0]
        });
      }
    });
});

I have the following errors:

{
  "code": 400,
  "error": {
    "message": "(#324) Requires upload file",
    "type": "OAuthException",
    "code": 324,
    "fbtrace_id": "E5FGvbwN2Gs"
  }
}