Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

post 'attached_media' array #187

Open
Art3miX opened this issue Feb 7, 2021 · 5 comments
Open

post 'attached_media' array #187

Art3miX opened this issue Feb 7, 2021 · 5 comments

Comments

@Art3miX
Copy link

Art3miX commented Feb 7, 2021

I was trying to post a post to Facebook with multiple images, but facebook was giving me error when I was adding attached_media parameter to the ${pageId}/feed, This is how I ended up doing it:

const postData = {
  message:        post.text,
};

images.forEach((imageId, index) => {
  postData[`attached_media[${index}]`] = `{"media_fbid": "${imageId}"}`
});

Looping over all the images, and manually adding attached_media[index] parameter.

It would be nice if we could just add it as an array, like this:

const postData = {
    message:        post.text,
    attached_media: [{"media_fbid": "${imageId1}"}, {"media_fbid": "${imageId2}"}]
};
@A-BMT02
Copy link

A-BMT02 commented Sep 15, 2022

Hey @Art3miX
I tried to post multiple images to my facebook page using your first method but i am getting an error. This is the data i'm sending

postData =  {
  caption: 'hello',
  'attached_media[0]': '{"media_fbid": "01"}',
  'attached_media[1]': '{"media_fbid": "02"}'
}

and this is the request i'm sending

        const b = await FB.api("me/photos", "post", postData);

and this is the error i'm getting

 response: {
    error: {
      message: '(#324) Requires upload file',
      type: 'OAuthException',
      code: 324,
      fbtrace_id: 'ANWCN2qEmLMW6-h-afDPMAy'
    }
  }

@Art3miX
Copy link
Author

Art3miX commented Sep 15, 2022

Hey @Art3miX I tried to post multiple images to my facebook page using your first method but i am getting an error. This is the data i'm sending

postData =  {
  caption: 'hello',
  'attached_media[0]': '{"media_fbid": "01"}',
  'attached_media[1]': '{"media_fbid": "02"}'
}

and this is the request i'm sending

        const b = await FB.api("me/photos", "post", postData);

and this is the error i'm getting

 response: {
    error: {
      message: '(#324) Requires upload file',
      type: 'OAuthException',
      code: 324,
      fbtrace_id: 'ANWCN2qEmLMW6-h-afDPMAy'
    }
  }

That was a long time ago, but I think you first need to upload the images, and use the given imageId in your post.
Note my first snippet, i do a loop over the uploaded images, and add them to the postData.

@A-BMT02
Copy link

A-BMT02 commented Sep 16, 2022

Hey @Art3miX I tried to post multiple images to my facebook page using your first method but i am getting an error. This is the data i'm sending

postData =  {
  caption: 'hello',
  'attached_media[0]': '{"media_fbid": "01"}',
  'attached_media[1]': '{"media_fbid": "02"}'
}

and this is the request i'm sending

        const b = await FB.api("me/photos", "post", postData);

and this is the error i'm getting

 response: {
    error: {
      message: '(#324) Requires upload file',
      type: 'OAuthException',
      code: 324,
      fbtrace_id: 'ANWCN2qEmLMW6-h-afDPMAy'
    }
  }

That was a long time ago, but I think you first need to upload the images, and use the given imageId in your post. Note my first snippet, i do a loop over the uploaded images, and add them to the postData.

Thanks for the reply. I appreciate
I did upload the images and received the ids back
I also added the Ids to postData just like you did in the first snippet
But for some reason i keep getting an error

@A-BMT02
Copy link

A-BMT02 commented Sep 16, 2022

After a bunch of trial and error , i fixed it!
In case anyone comes here from the future , heres what i did

When you make the first request you get an id and a post_id , Capture the ID NOT THE POST_ID

 const firstRequest = await FB.api("me/photos?published=false", "post", {
              source: fs.createReadStream(
              "{image-path}"
              ),
            });

Capture the Id's and store them in exactly this format

postData =  {
  caption: 'hello',
  'attached_media[0]': '{"media_fbid": "01"}',
  'attached_media[1]': '{"media_fbid": "02"}'
}

Send the final request to post to your page

 const secondRequest = await FB.api("me/feed", "post", postData);

Hope this helps someone
Also thanks for your time @Art3miX

@Art3miX
Copy link
Author

Art3miX commented Sep 16, 2022

Awesome, happy i could help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants