Upload post to Facebook fanpage with API - php

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'];

Related

Facebook App (canvas page) keeps redirecting to the index page

I am unable to figure out why every time I click on a link inside of my app (page 2 from page 1), it goes to page 2 but automatically redirects back to page one. This is getting frustrating. I feel like I have done everything correctly in the code below because it authenticates the user and everything as it should... but the only problem is, I can not navigate anywhere without it automatically going back to the index page.
$fb_app_id = "xxxxxxxx";
$fb_app_secret = "xxxxxxxxx";
$fb_scope = "email,user_birthday,user_location";
$fb_response_type = "token";
$canvas_page = "https://apps.facebook.com/app-name/";
require_once 'sdk/facebook.php';
$facebook = new Facebook(array(
'appId' => '' . $fb_app_id . '',
'secret' => '' . $fb_app_secret . '',
'cookie' => true,
));
$app_id = '' . $fb_app_id . '';
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . ("&scope=" . $fb_scope . "&response_type=" . $fb_response_type . "");
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
$UserId = $data["user_id"];
$token = $facebook->getAccessToken();
$facebook->setAccessToken($token);
$user_profile = $facebook->api('/me/');
} ...
Can someone look at this and tell me what it is that I am doing wrong?
Thanks in advance!
Problem solved....
require_once 'sdk/facebook.php';
$facebook = new Facebook(array(
'appId' => '0000000000000000000',
'secret' => 'if-i-tell-ya-ill-have-to-kill-ya',
'cookie' => true,
));
$canvas_page = "https://apps.facebook.com/whatever/";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $fb_app_id . "&redirect_uri=" . urlencode($canvas_page) . ("&scope=" . $fb_scope . "&response_type=" . $fb_response_type . "");
$signed_request = $_REQUEST["signed_request"];
$uid=$facebook->getuser();
if(empty($uid)){ echo("<script> top.location.href='" . $auth_url . "'</script>");}
else { // do something }

Curl connect to server issue(facebook api)

The error I'm getting is:
Curl error: couldn't connect to hostCurl error: couldn't connect to
host
I have tried almost everything to solve this problem but it doesn't seem to work file_get_contents doesn't work either but its enabled.
$response = curl_exec($ch);
if($response === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
$response = $response;
}
curl_close($ch);
return $response;
}
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$fanpage_id ='325145047546976';
$post_login_url = "http://www.feriajanos.com/src/teszt.php";
$photo_url = "http://www.feriajanos.com/kepek/mas/posztok/poszt20130412104817.jpg";
$photo_caption = "Feri A János";
$code = $_REQUEST["code"];
if (!$code) {
$dialog_url= "https://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream,manage_pages";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
if(isset($_REQUEST['code'] )) {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&redirect_uri=" . urlencode( $post_login_url)
. "&code=" . $code;
$response = curl($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// POST to Graph API endpoint to upload photos
$graph_url= "https://graph.facebook.com/me/photos?"
. "url=" . urlencode($photo_url)
. "&message=" . urlencode($photo_caption)
. "&method=POST"
. "&access_token=" .$access_token;
echo '<html><body>';
echo curl($graph_url);
echo '</body></html>';
}
?>

Facebook photo upload - An active access token must be used to query information about the current user

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();

Facebook Authorization runs in to a redirect loop

I am using the Facebook app canvas code in the documentation and it keeps redirecting after I hit "allow". Not sure what is wrong. Do check if it is a bug or it is just me. Its from
http://developers.facebook.com/docs/appsonfacebook/tutorial/
<?php
$app_id = ""; //Added my app id
$canvas_page = ""; //Added my canvas page
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
echo ("Welcome User: " . $data["user_id"]);
}
?>
my recommend is using php Sdk it makes every thing easier for you & the code for login will be like this :
<?php
include_once('src/facebook.php');
$config = array(
'appId' => 'xxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxx',
'fileUpload' => FALSE, // optional
);
$canvas_page = ""; //Added my canvas page
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
$uid=$facebook->getuser();
if(empty($uid)){ echo("<script> top.location.href='" . $auth_url . "'</script>");}
else { echo ("Welcome User: " . $uid);}

Posting Photo to FB results in Requires upload file error

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
);

Categories