Upload Image in apps profile page with facebook graph API - php

I am creating an app where the user can upload an image, that will be stored in apps profile page, and in the user profile. I'm using below code to do this work
<?php
$app_id = "XXXXXXXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXXXXXXXXXXXX";
$post_login_url ="XXXXXXXXXXXXXXXXXXXXXXX";
$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'];
// first get your album id, let's assume you need to create it
// create this before hand and you can just reference the id
$attachment = array('access_token'=> ACCESS_TOKEN, 'name'=>$ablum_name);
try{
$album_resp = $facebook->api("/{$this->page_id}/albums", 'POST', $attachment);
}catch(Exception $e){
throw new Exception("Failed to create album: ". $e->getMessage());
}
$album_id = $album_resp['id'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/photos?"
. "access_token=" .$access_token;
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>';
}
?>
With this code it is going to the user's profile but not to the app's profile page. Can any one please tell what is wrong with it? Or is there another way to do it?

Related

Facebook Malformed access token error 190

I'm trying to setup this basic uploaded which allows a user to upload a video directly to my Facebook group but I'm having a problem with the user access token, its throwing back a error "Malformed access token"
I grabbed a user access token from the Facebook graph api explorer and hard coded it into the URL and everything worked fine
I have the slightest idea as to what the problem might be
MY CODE:
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "http://DOMAIN/post.php";
$video_title = "TITLE FOR THE VIDEO";
$video_desc = "DESCRIPTION FOR THE VIDEO";
$group_id = "GROUP";
$code = $_REQUEST["code"];
echo '<html><body>';
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_actions";
echo('<script>top.location.href="' . $dialog_url . '";</script>');
}
$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);
$post_url = "https://graph-video.facebook.com/".$group_id."/videos?access_token=".$code."&"."title=".$video_title."&description=".$video_desc;
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>';
THE ERROR IS:
{
"error": {
"message": "Malformed access token AQDDR2mYCEP3F4MvRoBbHBhNViCPkdBhYu0IFyD3pkopzmIZlug69tzb3Cl7E1Z_5qzMTPBAnzMREGm0hU9Nym3EaIMCBelwhKOLwxSTQwStbZ7euPOxPLrfi9-JYSnJcjvy3K_13Ov14IaybzXfXhjNHLzZNuyofnFPbLVieYvOjfpT9UJSzS9TB6Plttbt0O4aKtnG9RkTaoNkJkwUgCw9IGV_dWw5vF77CrhyGxJ5B7quOZPjz39f5QtJ50X5njZE2C4jysMhg1Xfdg9vAhfaFYYEg4f_WrxVfF2QnHINHLLfhFbzSW74jJnEBoVszseA46nqWG4mnrgrs9K97jQ0iqc0UnCEY1JU92D94fX_kw",
"type": "OAuthException",
"code": 190,
"fbtrace_id": "DCS2/KjriCI"
}
}
I cleaned the access token and placed it in the post url and everything is working now
$app_id = "[APP_ID]";
$app_secret = "[APP_SECRET]";
$my_url = "[URL_TO_THIS_SCRIPT";
//$video_title = "TITLE FOR THE VIDEO";
//$video_desc = "DESCRIPTION FOR THE VIDEO";
$group_id = "[GROUP_OR_PAGE_ID]";
$code = $_REQUEST["code"];
echo '<html><body>';
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_actions";
echo('<script>top.location.href="' . $dialog_url . '";</script>');
}
$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);
$access_token_Clean = strtr ($access_token, array ('{"access_token":"' => '', '","token_type":"bearer"}' => ''));
$post_url = "https://graph.facebook.com/".$group_id."/photos?access_token=".$access_token_Clean;
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file" accept="image/*">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
echo '</body></html>';

Post Image to the Facebook using Graph API

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

Facebook Wall Status

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!

Upload video to facebook with php-sdk graph api

I have seen an example in facebook documentation.
$post_url = "https://graph-video.facebook.com/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
But i want to do this in facebook php-sdk.
$facebook->api('/me/videos/');
But it seems the video server is https://graph-video.facebook.com.
So how do i do this in graph api using php-sdk?
through graph its much more simple
To a group you can do this:
<?php
$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";
$group_id = "YOUR_GROUP_ID";
$code = $_REQUEST["code"];
echo '<html><body>';
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream";
echo('<script>top.location.href="' . $dialog_url . '";</script>');
}
$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);
$post_url = "https://graph-video.facebook.com/" . $group_id . "/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $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>';
echo '</body></html>';
?>
to a page you can do this
<?php
$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"; // 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_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;
}
}
// 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>';
?>

https://graph-video.facebook.com/ Redirect After Uploading

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

Categories