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>';
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
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>';
?>
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 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?
I'm starting an application in Facebook, and I'm having problems to get some user information.
The information I can't get is the following:
hometown_location
current_location
work
Here is my code:
<?php
$app_id = "";
$app_secret = "";
$my_url = "";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=email,offline_access,publish_stream,user_birthday,user_education_history,user_location,user_hometown,user_relationships,user_relationship_details,user_work_history&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name.'<br />');
echo("id " . $user->id.'<br />');
echo("mail " . $user->email.'<br />');
echo("birthday " . $user->birthday.'<br />');
echo("sex " . $user->sex.'<br />');
echo("hometown_location " . $user->hometown_location.'<br />');
echo("relationship_status " . $user->relationship_status.'<br />');
echo("current_location " . $user->current_location.'<br />');
echo("education " . $user->education.'<br />');
echo("work " . $user->work.'<br />');
}
else {
}
?>
I got the sex with "gender", but I can't figure out the others.
The properties you want are hometown and location. Hometown requires the user to grant user_hometown and Location requires the user to grant user_location permissions to your app:
....
"username": "alienwebguy",
"hometown": {
"id": "112276852118956",
"name": "Maple Grove, Minnesota"
},
"location": {
"id": "102825596420583",
"name": "Antioch, California"
},
....
Per the Open Graph "User" API docs:
hometown The user's hometown user_hometown or friends_hometown object containing name and id
location The user's current city user_location or friends_location object containing name and id