I have the following code which I think is very close to working but I keep getting the following error:
{"error":{"message":"(#324) Requires upload file","type":"OAuthException"}}
but I cannot figure out how to specify the upload file... I have tried both image and source, both give the same error. Any thoughts, help, suggestions would be greatly appreciated as I have been struggling with this for a couple of days and it seems like it should not be this hard...
<html>
<head>
<title>Photo Upload</title>
</head>
<body>
<?php
$app_id = "MY APP ID GOES HERE";
$app_secret = "MY APP SECRET GOES HERE";
$post_login_url = "MY URL GOES HERE";
$album_name = "Test Album";
$album_description = "Blah Blah event Photos";
$photo_source = realpath("C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg");
$photo_message = 'Here is the lighthouse';
$code = $_REQUEST["code"];
echo "code ==>" . $code . '<br/><br/>';
//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,user_photos";
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'];
echo "access_token ==>" . $access_token . '<br/><br/>';
// 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; //upload photo
echo "album_id ==>" . $album_id . '<br/><br/>';
// Upload the photo
$args = array( 'message' => $photo_message,
'image' => $photo_source
);
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$album_id.'/photos?
access_token='.$access_token;
echo "url ==>" . $url . '<br/><br/>';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
echo "data ==>" . $data . "<br>";
}
?>
</body>
</html>
You need to set the config var fileUpload to true when creating a new instance of the Facebook class as described in the documentation here and here. And you also need the photo_upload permission ;-)
require_once("facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = true; // <-- set this to 'true' otherwise it won't work
$facebook = new Facebook($config);
You are using file name instead of file itself in your Graph API call.
You should prefix file name with #
$args = array( 'message' => $photo_message,
'image' => '#'.$photo_source
);
Related
I wrote a code to upload videos to my Facebook fanpage, but it gets uploaded to the community of my fanpage.
May someone help me, what is the problem with my code?
My code:
$appId = 'xxxxxxxxxxx';
$appSecret ='xxxxxxxxxx';
$my_url = ':my website url';
$perms_str = "publish_actions";
$videoPath = 'xxxxxxxxxx';
$title = 'xxxxxxxxxx';
$descriptions = 'xxxxxxxxxxx';
if (!isset($_GET['code'])) {
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $appId . "&redirect_uri=" . urlencode($my_url)
. "&scope=" . $perms_str;
echo("<script>top.location.href='" . $auth_url . "'</script>");
} else {
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $appId . "&redirect_uri=" . $my_url
. "&client_secret=" . $appSecret
. "&code=" . $_GET['code'];
$response = file_get_contents($token_url);
$access_token = Json::decode($response)['access_token'];
$fb = new Facebook([
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.10',
]);
$pageId = 'xxxxxxxxxxxx';
$fileToUpload = $videoPath ;
$whereUpload = '/'.$pageId.'/videos';
$data = [
'title' => $_SESSION['fileToUpload']['title'],
'caption' => $_SESSION['fileToUpload']['descriptions'],
'source' => $fb->fileToUpload($fileToUpload)
];
}
try {
$response = $fb->post($whereUpload, $data, $access_token);
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
thank you cBroe , i added this code and this worked
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $appId . "&redirect_uri=" . $my_url
. "&client_secret=" . $appSecret
. "&code=" . $_GET['code'];
$response = file_get_contents($token_url);
$access_token = Json::decode($response)['access_token'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,
'https://graph.facebook.com/v2.10/'.$pageId.'/?
access_token='.$access_token.'&debug=all&fields=access_token&format=json
&method=get&pretty=0&suppress_http_code=1');
$content = curl_exec($ch);
$pageAccessToken=Json::decode($content)['access_token'];
I have created a facebook app in last month.
I am trying to upload a video from my PHP code, but it throws an error that (#353) You must select a video file to upload. While I tried to upload the same video from my Facebook account directly and it gets uploaded properly.
I don't know what is wrong things that exists, PHP code is as below
$api="/me/videos";
$uploaded_videos=$facebook->api($api);
$video_file_path=$user_dir_abs_path."/NewProject20.mov";
if(file_exists($video_file_path))
{
echo "file exists...";
}else{
die("not exist");
}
$ret_obj = $facebook->api('/me/videos', 'POST', array(
'source' => '#' . $video_file_path,
'title' => "This is just a test",
'description' => 'test9000',
'privacy' => json_encode(array('value' => 'EVERYONE' )),
)
);
echo '<pre>'. $ret_obj.'</pre>';
Video I have uploaded is here
Document I refer to code is here
https://developers.facebook.com/blog/post/493/
https://developers.facebook.com/blog/post/608/
I have used following code as well, but I am getting that same error..
$id=$facebook->getUser(); /* UID of the connected user */
$api="/".$id."/videos";
echo "api -> $api";
/*$ret_obj = $facebook->api('/me/videos', 'POST', array(*/
$ret_obj = $facebook->api($api, 'POST', array(
'source' => '#' . $video_file_path,
'title' => "This is just a test",
'description' => 'test9000',
'privacy' => json_encode(array('value' => 'EVERYONE' )),
)
);
echo '<pre>'. $ret_obj.'</pre>';
From your comments, I got to know that you need to upload/post a video from your server to facebook, instead form posting method specified in documentation.
I don't know much about facebook-sdk, I would suggest you to use CURL method instead.
<?php
$app_id = "XXXXXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXXXXXXXXXXX";
$my_url = "http://localhost/url_of_this_page.php";
$video_title = "Video title here";
$video_desc = "Video description here";
$code = $_REQUEST["code"];
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/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
$ch = curl_init();
$data = array('name' => 'file', 'file' => '#'.realpath("ipad.mp4"));// use realpath
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
if( $res === false ) {
echo curl_error($ch);
}
curl_close($ch);
?>
As a response from facebooks, you'l get video id like:
{"id":"892161284142980"}
Have a look at https://developers.facebook.com/docs/graph-api/reference/v2.0/user/videos/#publish
Videos must be encoded as multipart/form-data and published to
graph-video.facebook.com instead of the regular Graph API URL.
If anyone is still having issue uploading videos via the facebook sdk4 api, the below code worked for me:
$file_name = "/absolute/path/to/file/in/directory";
$linkData = [
'source' => new \CURLFile($file_name, 'video/m4v'),
'message' => 'Your message',
];
$ret_obj = $facebook->api('/me/videos', 'POST', $linkData);
I am trying to echo my name but I got an error: Trying to get property of non-object. Please help. I also have problem getting the user access token though I have no problem getting the app access token.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
require_once('facebook.php');
$config = array();
$config['appId'] = "MY_APP_ID";
$config['secret'] ="MY_APP_SECRET";
$facebook = new Facebook($config);
$app_id = "MY_APP_ID";
$app_secret = "MY_APP_SECRET";
$my_url = "http://localhost:8080/samplewebsite/index.php/";
$code = $_REQUEST['code'];
if(empty($code))
{
$_SESSION['state'] = md5(uniqid(rand(),TRUE));
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=" . $_SESSION['state']
. "&scope=publish_actions,read_stream";
header("Location: " . $dialog_url);
}
else
{
echo "Hello, your OAuth code is " . $code . "<br>";
}
if ($_REQUEST['state'] == $_SESSION['state'])
{
$facebook->api('oauth/access_token',array(
'client_id' => $app_id,
'client_secret' => $app_secret,
'type' => 'client_cred',
'code' => $code,
));
$token = $facebook->getAccessToken();
echo "<br>" . "Hello, Your access_token is: " . $token;
$graph_url = "https://graph.facebook.com/me?access_token=" . $token;
//$user = json_decode(file_get_contents($graph_url));
//echo $user->name;
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$user = json_decode(curl($graph_url));
echo $user->name;
}
?>
</body>
</html>
You are trying to echo $user->name but json_decode will not return an object by default. It returns an associative array unless you pass in TRUE as a second parameter.
So if you change:
$user = json_decode(curl($graph_url));
to:
$user = json_decode(curl($graph_url), TRUE);
It might work as you want it to. If not, try looking at what your curl() call returns and then what $user actually contains with something like var_dump().
I have this problem with my code, iam trying to upload an image to a users wall, but it give me following error:
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /var/www/parkourjams.dk/emilaagaard/customers/fbgen/core/facebook/base_facebook.php on line 1238
My code is:
<?php
session_start();
require 'facebook/facebook.php';
$app_id = "438648619527874";
$app_secret = "HIDDEN";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'fileUpload' => true,
'cookie' => true
));
$post_login_url = "http://emilaagaard.dk/customers/fbgen/core/test.php";
$code = $_REQUEST["code"];
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;
// curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
echo "Token: " . $access_token . "<br /><br />";
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
// Post it!
$user = $facebook->getUser();
$args = array(
'message' => 'Photo Caption',
'image' => '#'.realpath("ac.png")
);
$user = $facebook->getUser();
$user_profile = $facebook->api('/me');
if ($user) {
print_r($user);
echo "Username: " . $user_profile['username'];
try {
// We have a valid FB session, so we can use 'me'
$data = $facebook->api('/me/photos', 'post', $args);
echo $data;
} catch (FacebookApiException $e) {
print $e;
}
} else {
echo "Dead user";
$loginUrl = $facebook->getLoginUrl();
print ', try login</script>';
}
echo '</body></html>';
}
?>
I really hope for someone out there can help me, iam tired and have probably code-blinded my self for staying up all night after a party to finish this.
Thanks!
if you are testing it on local server
insert
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
after curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
insert
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
$facebook->setAccessToken($access_token);
before $user = $facebook->getUser();
I am trying to create a PHP page that will create a photo album and upload a photo. So far, it authenticates and creates a photo album without any problem but I cant seem to get it to upload the photo. I keep getting the following error:
Warning: fopen(https://graph.facebook.com/yyyyyyyyyyyyyyyyy/photos?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/content/30/8734730/html/uploadPhoto.php on line 96
<html>
<head>
<title>Photo Upload</title>
</head>
<body>
<?php
$app_id = "xxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$post_login_url = "http://xyz.com/uploadPhoto.php";
$album_name = "Brand Image Productions";
$album_description = "Blah Blah event Photos";
$photo_source = 'C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg';
$photo_message = 'Here is the lighthouse';
$code = $_REQUEST["code"];
echo "code ==>" . $code . '<br/><br/>';
//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,user_photos";
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'];
echo "access_token ==>" . $access_token . '<br/><br/>';
// 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; //upload photo
echo "album_id ==>" . $album_id . '<br/><br/>';
// Upload the photo
$graph_url = "https://graph.facebook.com/" . $album_id . "/photos?access_token=" . $access_token;
echo "graph_url ==>" . $graph_url . '<br/><br/>';
echo "photo_message ==>" . $photo_message . '<br/>';
echo "photo_source ==>" . $photo_source . '<br/><br/>';
$postdata = http_build_query(
array(
'message' => $photo_message,
'source' => $photo_source
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=> 'Content-type: application/x-www-form-urlencoded',
'enctype' => 'multipart/form-data',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$fp = fopen($graph_url, 'r', false, $context);
fpassthru($fp);
fclose($fp);
}
?>
</body>
</html>