I see that I can access a friend's groups via the Graph API:
https://graph.facebook.com/11111111/groups
... but how do I access it via FQL? (I am searching the documentation extensively before asking these questions, sorry if I'm asking an obvious one).
if you want to get the groups of user with the id 1111111
you can write this query:
select gid, name from group
where gid in (select gid from group_member where uid = 1111111)
don't forget you need to have the friends_groups permission in order to get this data
the documentation for those tables can be found here:
http://developers.facebook.com/docs/reference/fql/group_member/
http://developers.facebook.com/docs/reference/fql/group/
Yeah. I tried it did not work with FQL . But you can use the following code to get it via graph api
function get_groups($facebook)
{
require_once 'config.php';
global $facebook;
$groups = $facebook->api('/me/groups');
var_dump($groups);
return $groups;
}
The config file has the following :
<?php
require_once("facebook.php");
$app_id = "";//put the app id
$app_secret = "";//put app secret code inside " "
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if(is_null($facebook->getUser()))
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");//include the permission you want here
exit;
}
?>
Related
While developing facebook apps I want to fetch the liked status of a page to redirect them accordingly.Earlier I used the following code:
<?php
require 'facebook.php';
$app_id = "**************";
$app_secret = "*************************************";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
if($like_status==1)
echo "liked";
else
echo "not liked";
?>
But due to the new policy by facebook, in the apps developed after 7th aug 2014 I cannot get the page liked status using this method.
Pages.isFan() is also not working and FQL I read will not last long. Is there any other method to do it?
https://developers.facebook.com/docs/apps/changelog
Fangates are dead, they donĀ“t work for new Apps anymore, and there is no other way to do it. They also forbid them in the Platform Policy (see Changelog):
You must not incentivize people to use social plugins or to like a
Page.
That is why the like value in signed_request was removed.
What I am actually trying to do is to retrieve my posts/statuses from my personal facebook profile page (this is not a fan page) and display them in my website. Also please note that every user that will visit my website should be able to view my statuses by means it does not have to do with authenticating them or something like that. This is something that needs to be generated server-side.
I am aware that you can retrieve a JSON string from a URL like this https://graph.facebook.com/MY_PROFILE_ID/feed?access_token=ACCESS_TOKEN because I tried it from the facebook developers (Graph API Explorer) page and it is exactly what I need.
The thing is that when I generate the access token from the Graph API Explorer I can select permissions and it generates the token respectively according to permissions chosen, such as user_status, status_update, etc.
Now I want to accomplish this by using PHP-SDK but I have no idea how to generate an access token the same as the one I generate in the Graph API Explorer.
The basic way to do this is by calling getAccessToken() as shown here. The thing is that when I use the token generated from this simple method the JSON string returned will only show me my basic information.
$config = array(
'appId' => 'MY_APP_ID',
'secret' => 'MY_APP_SECRET',
);
$facebook = new Facebook($config);
$ACCESS_TOKEN = $facebook->getAccessToken();
How can I add permissions for instance? Should I assign parameters somewhere? I spent quite a lot of time reading the facebook API documentation and some other forums but I did not find the answer that I need.
Finally if I get the right access token than I would simply retrieve the JSON string and parse it with what I need.
Thanks.
you need for add permissions in your code for login using the fb js sdk add in the scope this like this example:
$("#login").on('click',function(){
FB.login(function(response){
console.log(response);
},{scope: 'email,manage_pages,read_insights'});
});
or in php fb sdk the next form:
<?php
require 'server/fb-php-sdk/facebook.php';
$app_id = 'APP_ID';
$app_secret = 'APP_SECRET';
$app_namespace = 'APP_NAMESPACE';
$app_url = 'https://apps.facebook.com/' . $app_namespace . '/';
$scope = 'email,publish_actions';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get the current user
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Login Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
?>
the most important for add permission is this part :
$scope = 'email,publish_actions';
you can find all about the extended permissions in this link
https://developers.facebook.com/docs/reference/login/extended-permissions
I have built an app using the PHP Facebook SDK. It allows users to authenticate my app with facebook OAth so that they can update their status' via my App. This works great, however a lot of my users have business pages and they want to update the status on their business page not their main personal feed. How is this possible? Below is what I have so far.
if ($status != ''){
try {
$parameters = array(
'message' => "$status"/*,
'picture' => $_POST['picture'],
'link' => $_POST['link'],
'name' => $_POST['name'],
'caption' => $_POST['caption'],
'description' => $_POST['description']*/
);
//add the access token to it
$parameters['access_token'] = $access_token;
//build and call our Graph API request
$newpost = $facebook->api(
'/me/feed',
'POST',
$parameters
);
$success['status'] = "$xml->status";
$xml2 = new XMLGenerator($success, 'facebook','status');
$returnData = $xml2->output;
$returnData = APIResponse::successResponse('200', "$xml->status");
} catch (Exception $e) {
$returnData = APIResponse::errorResponse('400', 'Facebook Error: '.$e);
}
I assume I would have to change '/me/feed'? but to what? What is they have multiple pages how would my app know which page to post to?
Any help with this would be much appreciated.
You can substitute me with the PAGE_ID to post to a page e.g., /013857894/feed. Make sure that you have completed the OAuth process with the manage_pages and publish_stream permissions. You can learn more at the link below:
https://developers.facebook.com/docs/reference/api/page/#statuses
If the user has multiple Pages then you will first need to give them some way of selecting which page they want to post to. You can find out which Facebook Pages a given user is the administrator of by calling /me/accounts for that user. You can find out more about this approach in the Connections section of this page:
https://developers.facebook.com/docs/reference/api/user/
I have this PHP code that i need help with
here is what i have
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET,
'cookies' => 'true'
));
$FBuser = $facebook->getUser();
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . FB_APP_ID .
"&client_secret=" . FB_APP_SECRET .
"&grant_type=client_credentials";
So far so good.. but then i try
$permissions = $facebook->api("/me/permissions");
And i get the error
"An active access token must be used to query information about the current user."
I know the user has already installed my app. I must be missing something simple??
Thanks
Randy
This:
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . FB_APP_ID .
"&client_secret=" . FB_APP_SECRET .
"&grant_type=client_credentials";
Simply gets an access token for the APP. There is no user attached to this. Read this documentation about getting a user's login url. Then, the facebook SDK you have at that redirect_uri will finish the job for you, and get you an access_token. From there you just need to call $permissions = $facebook->api("/me/permissions"); and it should work just fine.
To be sure, after you get that access_token in your current script, run it in the debugger to ensure a user_id is tied to it.
I'm trying to add a tab to a fanpage using the graph api/PHP SDK and I'm receiving an error :
(#210) Subject must be a page I've tried using both the user access_token AND the page access_token but neither work. I've tried using the page id of numerous accounts and still no go. Here is my code:
<?php
$path="/PAGE_ID/tabs/";
$access_token="ACCESS_TOKEN";
$params = array(
'app_id' => "APP_ID",
'access_token' => $access_token
);
try{
$install = $facebook->api($path, "POST", $params);
}catch (FacebookApiException $o){
print_r($o);
}
?>
And here is the error I get:
FacebookApiException Object
(
[result:protected] => Array
(
[error] => Array
(
[message] => (#210) Subject must be a page.
[type] => OAuthException
)
)
[message:protected] => (#210) Subject must be a page.
[string:Exception:private] =>
[code:protected] => 0
Thanks for any help you can provide!
If you are not limited to using the API to add your application to your page then you can follow the instructions provided by Facebook at this link :
https://developers.facebook.com/docs/reference/dialogs/add_to_page/
Essentially you can use a dialog ( see the link above ) or this direct URL to add tab apps to your page :
https://www.facebook.com/dialog/pagetab?app_id=YOUR_APP_ID&display=popup&next=YOUR_URL
Dont forget to substitute APP_ID for your app id and next for a different URL
API Call is atm bugged: https://developers.connect.facebook.com/bugs/149252845187252?browse=search_4f31da351c4870e34879109
But here is a solution for JS: OAuthException "(#210) Subject must be a page." - just do not use the library and do your own call.
I did it with PHP:
<?php
$url = 'https://graph.facebook.com/<PAGE ID>/tabs?app_id=<APP ID>&method=POST&access_token=<PAGE ACCESS TOKEN>&callback=test';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Echo value should be something like "test(true)".