PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 3, 2022

[FIXED] How can I post a photo and caption in the same publish on a facebook group api using FormData and axios?

 November 03, 2022     facebook-graph-api, javascript, typescript     No comments   

Issue

Someone could help me, I'm trying to send a photo and caption into a group, but isn't working! I'd like send the photo that I recieve as base64 and send to a facebook group api.

What I'm doing? I got the base64 then convert into buffer and write it on a local disk, then I read it into a formdata. I load the data into a form this way =>

const form = new FormData();
    const fileContent = Buffer.from(url as any, 'base64');

    fs.writeFile('../tmp', fileContent, (err) => {
      if (err) return console.log(err)
    })


form.append('groupId', groupId)
 form.append('caption', caption) 
form.append('image ', fs.createReadStream('../tmp'))

In below is the axios configurations and request

 await client.post(`${config.facebook.BASE_URL}/${groupId}/photos`, form, {
  headers: {
    ...form.getHeaders(),
    Authorization: `OAuth ${config.facebook.token}`,
    'content-type': 'multipart/form-data',
    file_type: "image/jpeg",
  }

})

Note: This way I got the Error: Request failed with status code 500


Solution

I already resolved this although I changed de way that the file came to me, instead of receiving the image in base64 I'm receiving a signed url from google storage, and on the form.append, it's necessary to pass the name and the extension of the file, like this => form.append('source', image, 'file.jpg'); of course together with the other params and axios' configurations



Answered By - Jhonatan Sena
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing