Im creating a Facebook application which will ask to like page before continue with the app. But after changing Facebook api platform to version 2, I cant get Facebook user id, I get scoped user id instead. Using scoped_user_id i cant get users who liked my page. Heres my code
if ($fb_scope_id){
try {
$user_like = $facebook->api('/'.$fb_scope_id.'/likes/');
} catch (FacebookApiException $e) {
$user_like = null;
}
}
It returns an empty array. Any solution?
Related
I'm using a Facebook application to create a kind of "social sharing" platform.
The website version uses Facebook API, Twitter OAuth and Instagram, and the canvas version only uses Facebook API.
I know that, when using the Canvas, Facebook sends a signed_request parameter, which actually recognizes that I'm using the Canvas version and not the website version. So, this is my login code:
login.php
$is_canvas=$SController->checkIsCanvas();
if(isset($_REQUEST['request_ids'])) {
$_SESSION['auth_request']=strip_tags($_GET['request_ids']);
}
socialclass.php
function checkIsCanvas() {
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch (FacebookRequestException $ex) {
return $ex->getMessage();
} catch (\Exception $ex) {
return $ex->getMessage();
}
if ($session) {
$_SESSION['is_canvas']='true';
$accessToken=$session->getAccessToken();
$_SESSION['auth_tk_facebook']=$accessToken->__toString();
$request = new FacebookRequest(
$session,
'GET',
'/me?fields=id,name,first_name,last_name,birthday,email,age_range,location,gender'
);
$response = $request->execute();
$userProfile = $response->getGraphObject(GraphUser::className());
if(isset($userProfile)) {
$login=$this->checkFacebookLogin($userProfile);
if($login===false) {
return $userProfile;
} else {
header("Location:".getBASEURL());
}
} else {
return false;
}
} else {
return false;
}
}
The problem is that when I logout from canvas and then re-login, Facebook hasn't sent any signed_request parameter, so I can't recognize if I'm actually using the Canvas version or the website's one. To temporally solve this, I just reload the page, so Facebook can save another signed_request, but it's quite annoying.
This problem comes up also when i go from the Canvas version to the website version without logging-out. Obviously, the is_canvas session variable doesn't unset, so the website version appears equal to the Canvas version.
How can I always check if I'm in the canvas, or in the website without reloading after logging-out? The problem should be, I hope, easy to solve, but I've not figured out how to do that yet.
If you have any questions about more pieces of code or if I wasn't as clear as I'm expecting ( sorry for my English, too ) don't worry about asking :)
Thanks in advice.
Similar/related Question is solved here : Check if site is inside iframe
Because canvas is always called in iframe. Please check if it helps.
I am the administrator of a Facebook page which needs to have an age restriction of 17+ on it.
On my website, I am making some Graph API queries, like querying photo albums and linking to them on Facebook. I am using the PHP Facebook API.
I can't seem to get to grips around the page access tokens and when they expire and when not.
I tried to follow this answer here, which was highly voted, but it isn't clear what you do with the first access_token after you copy it. I am not sure if this is even valid any more.
I tried to access the page using the access token I get when I do me/accounts in Graph Explorer and it works, but after some time it expires and I get an exception that the token expired. I need something that stays working and doesn't expire and need me to go in and update it.
This is the code I have so far:
$fbconfig = array();
$fbconfig['appId'] = DEF_APP_ID;
$fbconfig['secret'] = DEF_APP_SECRET;
$fbconfig['fileUpload'] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
$facebook = new Facebook($fbconfig);
//I am not sure if the following is the right way
$facebook->setAccessToken(DEF_ACCESS_TOKEN);
$facebook->setExtendedAccessToken();
...
try
{
$albums_resp = $facebook->api('/'.$id.'/albums','GET');
...
}
catch (FacebookApiException $ex)
{
error_log($ex);
}
What is the right way to achieve this?
I know, this question was asked hundred times, but none of the given solutions works for me.
I'm trying to post on the wall of every page the current user owns. So, I first get the permissions to publish_actions, read_stream, manage_pages and offline_access. Then I check via /me/permissions, that the user really granted those permissions.
Then, I try to get all pages of the user and post on the wall of the page, if the user is the admin:
$accounts = $facebook->api("/me/accounts", "GET", array("access_token"=>$facebook->getAccessToken()));
$data = $accounts["data"];
foreach ($data as $page)
{
if (isset($page["perms"]) && in_array("CREATE_CONTENT", $page["perms"])))
{
$facebook->api("/".$page["id"]."/feed", "POST", array("link"=>$link, "access_token"=>$page["access_token"]));
}
}
But, posting on the wall of the page fails, with the well known error message
Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action
Any ideas? Where's my fault?
If you can make the same call via the Graph API Explorer or cURL for one of the pages then your logic is right. So I would place a try/catch on the page/id call to see if a specific page is messing up the loop.
try {
$facebook->api("/".$page["id"]."/feed", "POST", array("link"=>$link, "access_token"=>$page["access_token"]));
} catch (FacebookApiException $e) {
error_log($e . " for: " .$page["id"] );
}
}
Im using the SDK 3 by facebook, and to store the facebook id i am doing this:
$facebook_id = $user;
Which works a treat! However... i cannot access individual items in the array, like i remember with the previous SDK you could do:
$first_name = $me['first_name'];
However, i have tried that, and $user['first_name'] and stuff but getting no where! I cant find any documentation of this either on their dev website or on the internet :/
Many thanks.
Ian.
Seems like $user is just the Facebook User ID that you get through the authentication flow. If you want to get user info, you need to make a separate Graph call, like so:
$res = $facebook->api('/me');
$first_name = $res['first_name'];
How do i grab the latest status update from a facebook page in php? Not a users profile but a page (that you can like)? The documentation is a nightmare. Having trouble finding an answer to this!
Pages are just the same as profiles in terms of accessing them though the graph. To get the wall posts from a page you use the url https://graph.facebook.com/PAGE_ID/feed.
Adding a limit parameter will return only the most recent: https://graph.facebook.com/PAGE_ID/feed?limit=1.
Here's a quick and dirty example of how this would work in PHP:
try {
$feed = $facebook->api('/PAGE_ID/feed?limit=1');
} catch (FacebookApiException $e) {
error_log($e);
}