Facebook Graph API: Publish post with multiple videos and photos
Asked Answered
M

3

5

I try to publish a post with multiple videos and photos by using the PHP SDK. I uploaded videos and photos using batch request and got the id. Then I pass the media ids along with post data using attached_media. Things work fine for single or multiple photos. But not for a single video or multiple videos. I got this error: "Graph returned an error: (#10) Application does not have permission for this action" whenever ids of videos are included in attached_media.

Here is the code that I used:

$fb = $this->init(); try{ // Returns a Facebook\FacebookResponse object        
$publishData = [ 'message' => $post['content']];

    if(count($media_ids) > 0){
        $publishData ['attached_media'] = [];
        foreach($media_ids as $key => $media_id){
            array_push($publishData['attached_media'],'{"media_fbid":"' . $media_id . '"}');
        }
    }
    $response = $fb->post(
        '/me/feed',$publishData
        ,
        $accessToken
    );
}
catch(FacebookResponseException $e){
    echo 'Graph returned an error: ' . $e->getMessage();
    echo $e->getTraceAsString();
    exit;
}
catch(FacebookSDKException $e){
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    echo $e->getTraceAsString();
    exit;
}
$graphNode = $response->getGraphNode();

Is there anyway to solve this. Thank you.

Multipartite answered 14/11, 2017 at 11:5 Comment(2)
Did you find a way?Elianaelianora
Did you find a way?Geology
V
6

The truth is that you can't mix photos and videos. Facebook's API doesn't allow that. You can make:

  • A post containing video + text (description)
  • A post containing multiple photos + text

To make a video post you have to hit

POST https://graph-video.facebook.com/v10.0/<page id>/videos?file_url=<public file URL>&access_token=<your access token>&published=true&description=<text>

The file_url and description should be URL encoded.

To post multiple photos you first do the photo "uploading" and as a result you'll have ids. Those ids you set to attached_media param.

Getting photo ids:

POST https://graph.facebook.com/<page id>/photos?url=<public file URL>&access_token=<access token>&published=false

Notice the published=false. That's important. Without that you'll make a page post containing a single photo.

And finally making the actual page post:

POST https://graph.facebook.com/<page id>/feed?message=<text>&access_token=<access token>&attached_media[0]={"media_fbid":"<id>"}&attached_media[1]={"media_fbid":"<id>"}

P.S. This approach assumes that you have your content uploaded somewhere else and you have public URLs where this content is available. If you have the raw files and you want to upload them to Facebook then you have to follow another approach.

Valeric answered 17/4, 2021 at 15:13 Comment(2)
Facebook allows a lot of things via the GUI. Those things are not possible via the API because they don't want to allow automation on a higher level.Valeric
It's not allowing me to when using photos and videos together in a single post. My question is it possible?Praxiteles
E
0

I know its been a while but I was having the same problem and it's 2020...

The only way I was able to show the video in the timeline of a page was using the following Facebook documentation that covers uploading & posting a video to a user's timeline with the Facebook SDK for PHP.

https://developers.facebook.com/docs/php/howto/example_upload_video

It's clearly not the best approach but at least the video appears in the timeline with some title and description.

$data =
[
    'title' => 'Your title',
    'description' => 'Your description'
];

$response = $fb->uploadVideo($pageId, $videoUrl, $data, $token);

Note: the videoUrlmust be a relative path as the FacebookFile class uses functions like filesize.

Eurhythmics answered 22/1, 2020 at 20:4 Comment(0)
S
0

Facebook doesn't directly allow to publish post with multiple videos and photos on the business page. However, it is possible on a personal page so as an alternate solution you can create a post on a personal page and share it on the business page.

Check this video for more information: https://www.youtube.com/watch?v=AoK_1S71q1o

Singleminded answered 20/12, 2020 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.