demo insys
demo insys

Reputation: 1

get facebook shared post post id and update post

i want to update a image every 30 seconds that i already share in my facebook feed but the api is asking postId but when i try to get postId via api it gives me post url so how can i update post

getting postId

 const tokenUrl = `https://graph.facebook.com/v14.0/oauth/access_token?client_id=${appID}&client_secret=${appSecret}&grant_type=client_credentials`;
      const tokenResponse = await fetch(tokenUrl);
      const tokenData = await tokenResponse.json();
      console.log("tokenData",tokenData)
      const accessToken = tokenData.access_token;

      console.log("token",accessToken)
  
      // Step 2: Retrieve the post ID
      const postIdUrl = `https://graph.facebook.com/v14.0/&id=${encodeURIComponent(imageURL)}&access_token=${accessToken}`;
      const postIdResponse = await fetch(postIdUrl);
      const postIdData = await postIdResponse.json();
       const postId = postIdData.id
      console.log("postId",postId)
      res.status(200).json({ postId });


try to update post

const requestBody = {
        url: "https://demo.edulabs.sg/assets/assets/images/test_pie_chart.png",
      };

      const apiUrl = `https://graph.facebook.com/v14.0/${postId}?method=put&message=${requestBody}&access_token=${accessToken}`;

      
            const response = await fetch(`${apiUrl}?access_token=${accessToken}`, {
              method: 'POST',
              headers: {
                'Content-Type': 'application/json',
              },
              body: JSON.stringify(requestBody),
            });
      
            if (response.ok) {
              console.log('Image updated successfully on Facebook.');
            } else {
              console.error('Failed to update the image on Facebook.');
            }

Upvotes: 0

Views: 67

Answers (0)

Related Questions