i'm trying to upload video on Facebook page using this code. I have added all the compulsory credentials to add video. but when i select video and click upload button it json request says you dont have permission to upload video. I tried to debug this problem but couldn't succeed. please help me out. thank you
<?php
$app_id = "xxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://xxxxxxxx/test.php";
$video_title = "test_video_for_app";
$video_desc = "test_description";
$page_id = "xxxxxxxxxxxxxxxx"; // Set this to your APP_ID for Applications
$code = $_REQUEST["code"];
echo '<html><body>';
if(empty($code)) {
// Get permission from the user to publish to their page.
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id . "&redirect_uri=" . urlencode($my_url). "&scope=publish_actions,manage_pages";
echo('<script>top.location.href="' . $dialog_url . '";</script>');
} else {
// Get access token for the user, so we can GET /me/accounts
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=". $app_id . "&redirect_uri=" . urlencode($my_url). "&client_secret=" . $app_secret. "&code=" . $code;
$access_token = file_get_contents($token_url);
$accounts_url = "https://graph.facebook.com/me/accounts?" . $access_token;
$response = file_get_contents($accounts_url);
// Parse the return value and get the array of accounts we have
// access to. This is returned in the data[] array.
$resp_obj = json_decode($response,true);
$accounts = $resp_obj['data'];
// Find the access token for the page to which we want to post the video.
foreach($accounts as $account) {
if($account['id'] == $page_id) {
$access_token = $account['access_token'];
break;
}
}
// Using the page access token from above, create the POST action
// that our form will use to upload the video.
$post_url = "https://graph-video.facebook.com/" . $page_id . "/videos?". "title=" . $video_title. "&description=" . $video_desc. "&access_token=". $access_token;
// Create a simple form
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
}
echo '</body></html>';
?>
this is the responce coming from facebook
{
"error": {
"message": "(#100) No permission to publish the video",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "H1uOf8K83lL"
}
}
so finally after a long research i got the solution. we just need to add permission from developers.facebook.com.
fisrt go to developers.facebook.com
go to tools and support and select graph api explorer
then select your application and click on get access token
add the permission publish_action in that and click ok
u will get access token.
copy thataccess token and paste in place of
$access_token = file_get_contents($token_url);
it will be like
$access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
then run your code and its done
Related
I got the code from the Facebook page to publish the Photo to the user's wall (account).
Below is my PostToFB.php file:
<?php
include_once "facebook.php";
ini_set("display_errors",0);
//configuring application to post.
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_REDIRECT_URL";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
But this is not working. I get below Output:
{
"error": {
"message": "(#200) Permissions error",
"type": "OAuthException",
"code": 200
}
}
Can you please help me to solve this?
Thank You,
This code works absolutely fine. Only probably reason seems to me is that you have not granted the permission for the photo upload. Check which permissions you have granted to your app from here: https://www.facebook.com/settings?tab=applications
If still didnt helped, you can create a new app and try again; since this code is correct.
Edit:
publish_stream is deprecated, try using publish_actions
Let me first thank you for allowing me to ask some question here. I hope you can help me with the following question.
I want to post some message on User's Facebook status. It is successful with the following code:
$params = array('access_token'=>$access_token, 'message'=>$message);
$url = "https://graph.facebook.com/".$fbuserid."/feed";
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
What do I need to add or do to post image and/or link affiliate to this post msg? Thank you!
In order to publish a photo to a user's album, you must have the publish_stream permission. With that grantedd, you can upload a photo by issuing an HTTP POST request with the photo content and an optional description to one these to Graph API connections:
https://graph.facebook.com/USER_ID/photos - The photo will be published to an album created for your app. We automatically create an album for your app if it does not already exist. All photos uploaded this way will then be added to this same album.
https://graph.facebook.com/ALBUM_ID/photos - The photo will be published to a specific, existing photo album, represented by the ALBUM_ID.
Example:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_POST_LOGIN_URL";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message"
type="text" value=""><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
Sample Output:
{
"id": "1001207389476"
}
Documentation: https://developers.facebook.com/docs/reference/api/photo/
Hope this helps!
I'm creating a FaceBook app where the user selects multiple images and the application generates a single image from them (PHP). I'm giving the generated image a semi-random name - $storage_url = $rack_directory . "rack_" . mt_rand() . mt_rand() . ".png"; (e.g. rack_2128639756968968165.png) and storing it temporarily as well.
I'd like the user to be able to upload the generated image to their Facebook profile directly from the page rather than downloading and then uploading it.
In looking over answers in this site as well as others it looks like I can use this:
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_POST-LOGIN_URL";
$album_name = 'YOUR_ALBUM_NAME';
$album_description = 'YOUR_ALBUM_DESCRIPTION';
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
// Get the new album ID
$album_id = $result->id;
//Show photo upload form and post to the Graph URL
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message" type="text"
value=""><br/><br/>';
echo '<input type="submit" value="Upload" /><br/>';
echo '</form>';
echo '</body></html>';
}
...to generate a unique album and upload the picture...
But - I don't want to display a form that has a user browse to and upload (after they right/control click and save the image from the page). Just take the existing generated image displayed on the page and submit that.
Is there a function that I could use so that this process occurs and the only form requirement is to submit and upload?
Thanks.
What you need is the Facebook SDK :https://developers.facebook.com/docs/reference/php/
This makes all those "Graph" calls much simpler.
Pasting from that page:
// Load the facebook SDK
require_once("facebook.php");
$config = array();
$config['appId'] = 'YOUR_APP_ID';
$config['secret'] = 'YOUR_APP_SECRET';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
try {
$uid = $facebook->getUser();
catch (FacebookApiException $e) {
// Not logged on - you should log them on. Various methods, but redirect to $facebook->getLoginURL() is simplest. Docs: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/
}
Once the user is logged on, you can upload also through the SDK:
// Now upload the file
try {
$facebook->setFileUploadSupport('http://MyDomain.com/');
$response = $facebook->api(
'/me/photos/',
'post',
array(
'message' => 'Image Cpation',
'source' => '#/path/to/image' // #-sign must be the first character
)
);
}
catch (FacebookApiException $e) {
error_log('Could not post image to Facebook.');
}
You cna use an album ID in place of "me" for uploading directly to a specific album. But working your way through the SDK will help a lot more than trying it all manually with the graph calls!
Sorry, It's not a complete "here's the code" answer (there's still a bit of work to go), but that should help you upload without prompting the user.
I am using the following code it successfully upload video but after uploading it show some ID page. Can any one help how to redirect page to my desired location after uploading video successfully.
Thanks in advance.
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_POST_LOGIN_URL";
$video_title = "TITLE FOR THE VIDEO";
$video_desc = "DESCRIPTION FOR THE VIDEO";
$page_id = "YOUR_PAGE_ID";
$code = $_REQUEST["code"];
echo '<html><body>';
if(empty($code)) {
// Get permission from the user to publish to their page.
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream,manage_pages";
echo('<script>top.location.href="' . $dialog_url . '";</script>');
} else {
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$accounts_url = "https://graph.facebook.com/me/accounts?" . $access_token;
$response = file_get_contents($accounts_url);
$resp_obj = json_decode($response,true);
$accounts = $resp_obj['data'];
foreach($accounts as $account) {
if($account['id'] == $page_id) {
$access_token = $account['access_token'];
break;
}
}
$post_url = "https://graph-video.facebook.com/" . $page_id . "/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&access_token=". $access_token;
echo "<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">";
echo "Please choose a file:";
echo "<input name="file" type="file">";
echo "<input type="submit" value="Upload" />";
echo "</form>";
}
You will have to make the video upload to facebook happen on your server side, instead of your visitors browser, so you can catch the output and send the visitors browser to the right place.
Your users will upload their files on your server first, then you can use curl to send the video to facebook, and after its finished you can redirect the user.
Examples:
Facebook Graph api video object: http://developers.facebook.com/docs/reference/api/video/
Command line Curl examples: http://facebook.stackoverflow.com/questions/5227607/posting-an-embedded-video-link-using-the-facebook-graph-api
Example with Facebook PHP api: http://facebook.stackoverflow.com/questions/9018213/video-post-on-timeline-not-playing-inline
I have a website with Facebook capabilities (Sending personal messages, posting to wall). I have a page with a form that posting an image to the users wall photos album.
My problem is that after posting the image (Posting the form) i'm redirected to this url:
https://graph.facebook.com/'ALBUM_ID'/photos?access_token=AAAEzcP64ySABAA3YYxBzRlrtUn..............
And in the browser I see this: (I removed the numbers for security)
{
"id": "ID NUMBER HERE",
"post_id": "POST ID NUMBER HERE"
}
How can I tell Facebook to redirct me back to my page.
My code is:
$app_id = APP_ID;
$app_secret = APP_SECRET;
$my_url = "MY_URL";
$page_id = "PAGE_ID"; // Set this to your APP_ID for Applications
$code = $_REQUEST["code"];
if(empty($code)) {
// Get permission from the user to publish to their page.
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream,manage_pages";
echo('<script>top.location.href="' . $dialog_url . '";</script>');
} else {
// Get access token for the user, so we can GET /me/accounts
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$accounts_url = "https://graph.facebook.com/me/accounts?" . $access_token;
$response = file_get_contents($accounts_url);
// Parse the return value and get the array of accounts we have
// access to. This is returned in the data[] array.
$resp_obj = json_decode($response,true);
$accounts = $resp_obj['data'];
// Find the access token for the page to which we want to post the video.
foreach($accounts as $account) {
if($account['id'] == $page_id) {
$access_token = $account['access_token'];
break;
}
}
$ALBUM_ID = "ALBUM_ID";
// Show photo upload form to user and post to the Graph URL
$image_post = "https://graph.facebook.com/" . $ALBUM_ID . "/photos?"
. "access_token=" .$access_token;
$video_post = "https://graph-video.facebook.com/" . $page_id . "/videos?"
. "title=testTitle" . "&description=testDescription"
. "&access_token=". $access_token;
?>
<table align="center">
<tr>
<td>
<?php
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.image_post.' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/>';
echo 'Say something about this photo: <br/>';
echo '<textarea id="fbText" name="message"
rows="4" cols="47">';
echo '</textarea><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
</td>
</tr>
<?php
I've tried adding &redirect_uri=" . urlencode($my_url) to the $image_post but that didn't change anything in the behavior and redirected me to the same page without going back.
Thanks.
You have 3 options as I see it:
1 . This only works if you don't care about the response coming back from facebook:
<iframe name="uploader" onLoad="locationChange(this)"></iframe>
<form method="POST" action="..." target="uploader">
...
</form>
funnction locationChange(ifrm) {
console.log("iframe location has changed to: ", ifrm.src);
}
The problem with that is that if the upload fails you won't be aware, meaning that you can not differentiate between a successful upload and a failed one.
The reason you can not read the data from the iframe is that your page has a different domain than of the iframe, and browsers block communication in this case due to the same origin policy.
2 . Upload the image to your server and then from php upload the image to facebook.
You can use this: Upload Photo To Album with Facebook's Graph API.
3 . You can upload the image using ajax, there are some techniques, for example: Ajax Image Upload without Refreshing Page using Jquery.