Facebook Graph API: Publish post with multiple videos and photos - php

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.

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.

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.

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

Related

Post video on a fan page on facebook using php-sdk

I am having videos uploaded by users on my live site. I have created an admin panel where the admin reviews all the videos and if he wish that video to be posted on facebook then he has a button as Post to Wall infront of each video.
I have seen this tutorial but still unable to post videos on a fan page. I also found one more tutorial but according to this we can upload video only if it is on our local machine.
My problem is I want to upload video which is on my server not on my local machine. How can I achieve this? Please help.
Code for upload on fb:
$message = array('source' => 'http://bizmoapps.com/icstories/uploads/video/52e113c822271vid-20130707-wa0000.mp4');
$url = '/' . $_POST['pageid'] . '/photos';
$result = $fb->api($url, 'POST', $message);
if ($result) {
echo 'Data posted successfully';
}

Facebook photo sharing (& October 2013 breaking changes)

I'm building on a website for a client and the core functionality of it is to share photos with other users over Facebook.
So, what I've done is:
user picks a photo he likes and clicks the "share" button
script triggers Facebook "Friend Picker" popup
user picks a friend he wants to share the photo with and clicks "OK"
script triggers ajax request to the server which uploads the specific photo on the timeline of chosen friend
Here's the php photo sharing code:
$facebook = new Facebook($config);
$facebook->getAccessToken();
$user_id = $facebook->getUser();
$body = array(
'source' => '#' . CURR_DIR . $photo->path,
'message' => ''
);
if ($user_id)
{
try
{
$result = $facebook->api('/' . $fbid . '/photos', 'post', $body);
}
catch (FacebookApiException $e)
{
echo $e->getMessage();
}
}
So far this works beautifully, so my question is:
After the Facebook's "October 2013 breaking changes", they will be "removing the ability to post to friends' timelines via API".
They advise usage of feed dialog's from that point on.
So - how to upload a photo on a specific user's timeline using feed dialog's?
Is this even possible (because looking at the feed documentation page, I don't think it is...)?
So - how to upload a photo on a specific user's timeline using feed dialog's? Is this even possible
No, its not possible. I think you are now left with just one option- to post the photo on the user's wall (instead of friends'), and tag him/her on the photo. To tag friends, you have to use parameter: tags. Ref: Photo

Access to page's photo albums from within web app using tokens?

Im using Facebook's PHP sdk for the first time in a clients site and I'm a little confused. I'm trying to get some data from the clients facebook page and inject it into their site. One of the objects I am trying to get is a list of the clients photo albums for a photo gallery.
<?php
$albums = $Facebook->api("/169639127532/albums?fields=id,name,cover_photo");
$photos = array();
$queries = array();
foreach($albums['data'] as $album) {
if(array_key_exists("cover_photo", $album))
$queries[] = array("method" => "GET", "relative_url" => "/".$album['cover_photo']."?fields=source");
}
$rawPhotos = $Facebook->api('?batch='.json_encode($queries), 'POST');
foreach($rawPhotos as $photo) {
//print_r(json_decode($photo['body'])); echo "<br /><br />";
$photo = json_decode($photo['body']);
$photos[$photo->id] = $photo->source;
}
As you can see, im getting a list of the albums, then using a batch request to get all the cover photos for those albums.
Using my clients page ID, this doesn't work, nothing is returned. However if I use another page's ID this works.
So I put this down to authorisation. I noticed that if I used the Graph API explorer with an access token I could get the photos. Because of this, I copied the Token I had generated with the API explorer, and set it globally on the site using the following function and this works.
$Facebook->setAccessToken($token)
Obivously this isn't a very robust solution as the tokens only exist for a finite amount of time. I tried getting a token for the app using the /oauth/access_token API call with my apps details and successfully got a token back. If I use this as the token I can no longer see the data I was getting with the temporary token from the graph explorer however.
I do not want users to have to login just in order to see the photo gallery on the site.
How do I go about getting a token for the app that would allow me to do this?
Thanks.

Is it possible to post a photo to Facebook by a link, not upload?

Is this possible to create a photo on user/page's timeline not by uploading said photo, but by providing a URL to an already uploaded image? My code so far is this:
$response = $fb->api ("/$page_id/photos", 'post', array (
'name' => $_description,
'source' => $_image_path
));
This results in OAuthException #324 "Requires upload file". Also, I'd like to photo to be immediately posted on the page's timeline - will it happen, or do I need to make a separate call for that?
You can switch parameter source for url, and give it a publicly reachable HTTP URL as value.
But be aware, this only works for photos not on Facebook already – feeding it the URL of a photo on Facebook will result in an error message saying that posting photos from their CDN is not possible.

Facebook Photo Upload into Album on Fan Page failed

I have written an Application what posts Photos on a FanPage into a Specific Album long time ago.
Now I didn't used for a half year. Now I had to set up the Application again and grant the extended Permissions to the Fan Page (streamm_publish) again.
But now I've a Problem, I'm using the old REST API what now gives me the error: An unknown error ocurred with the error code 1.
Then I tried to post throught the Facebook Graph api.
I tried to make a call to the Api /pageid/albumid/photos what's not working(Unknown path components)
I tried to makea a call to /albumid_from_my_page/photos then the Photos were posted to my profile
I tried to upload it to /pageid/photos what is the same as the one above
But the code fpr the REST Api worked well, what's the problem there, and why the new Graph Api isn't working how she should?(BUG?)
To post a photo to an album, this is the code:
$post_data = array(
"message" => "My photo caption",
"source" => '#' . realpath($file)
);
$album_id = "XXXXX";
$facebook->api("/$album_id/photos", 'post', $post_data);
Now I suppose to interact with the page albums you need a page access_token added to your $post_data array, for this check this answer.
You need take ACCESS_TOKEN page...
try:
http://graph.facebook.com/me/accounts?access_token= GET THIS TOKEN USING GRAPH API... OR MAKE USING the getAccessToken()...
and will see all pages and aplications that u have, find just application for this case and COPY TOKEN... and damn!!!
It's possible to see this token thought GRAPH API EXPLORER...
regards.

Categories