Whats is the use of this facebook function? - facebook

I am trying to figure out the id of facebook user currently logged in.
I used this code but I'm getting error.
$facebook = new Facebook_php_sdk(array(
'appId' => '226568227411676',
'secret' => '68bdfeb63d01e8122ed0a46d8fd5dfc1'
));
$rr = $facebook->getAccessToken();
$token = array(
'access_token' => $rr
);
$user_profile = $facebook->api('/me','GET',$token);
on this I am getting error saying "Uncaught OAuthException: An active access token must be used to query information about the current user".

This error occured because you trying to access user's information, using Application access token. Details here

Related

Login on facebook sdk without callback

I would like to know if it's possible to login on Facebook api without a callback URL.
What I want to do is really "simple":
- Login on Facebook.
- Post or Delete on the wall.
- Logout of Facebook.
This is my code for login and post:
$fb = new Facebook\Facebook([
'app_id' => 'xxxx',
'app_secret' => 'xxxx',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['publish_actions'];
$loginUrl = $helper->getLoginUrl(null, $permissions);
echo 'Log in with Facebook!';
try {
$accessToken = 'xxxx';
//$accessToken = $helper->getAccessToken();
//echo 'Log in with Facebook!';
$linkData = [
'link' => 'http://www.desarrollolibre.net/blog/tema/50/html/uso-basico-del-canvas',
'message' => $model->value,
];
$response = $fb->post('/feed', $linkData, $accessToken);
$graphNode = $response->getGraphNode();
The problem here is that I have to specify the access token getting directly from developers app, because $accessToken = $helper->getAccessToken() returns nothing to me.
Any help will be appreciate.
You can't "auto-login", you have to implement a proper login process. If you want to automate things (and you really should not autopost, because that is not allowed), you need to store a User Access Token somewhere and use it later. You may want to use an Extended User Token for this, because the default one is only valid for 2 hours. The Extended User Token is valid for 60 days.
More information about Tokens and how to generate them:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Post to facebook page from facebook page tab app

I want to post activities from page tab app to my own facebook page. I referred many documents to post message to app.
My php code :
$GLOBALS["facebook"] = new \Facebook_Facebook(array('appId' => $facebook_app_id, 'secret' => $facebook_secret,));
$page_info = $GLOBALS["facebook"]->api("/pageid?fields=access_token");
print_r($page_info);die;
if (!empty($page_info['access_token'])) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'TEST'
);
$postId = $facebook->api("/pageid/feed", "post", $args);
But pageinfo variable has only id. I'm not getting access_token. Any idea what else need to be done?
We need manage_pages permission which we can get it using https://www.facebook.com/dialog/oauth?client_id=client_app_id&client_secret=9ea3e2eff7c65b1fcf1a633da&redirect_uri=YOUR_REDIRECT_URL&scope=read_stream,publish_stream,offline_access,manage_pages.
After that using above code we will get access token which can be used to publish to our page

Get user's country at Facebook: getSignedRequest() results without 'user' field

Code:
$facebook = new Facebook(array(
'appId' => some_fb_app_id),
'secret' => some_fb_app_secret),
'cookie' => true,
));
$result = $facebook->getSignedRequest();
echo var_export($result,1);
Result:
array (
'algorithm' => 'HMAC-SHA256',
'code' => 'A.....l',
'issued_at' => 1354720771,
'user_id' => 'some_user_id',
)
I expect $result["user"]["country"] to be set. How can I fix this problem?
ps. by Facebook Graph API - how to get user country?
According to the facebook PHP SDK getSignedRequest() only gives you the signed request, just like the name suggests. User info is usually gotten through the Graph API, using calls like
$facebook->api('/me','GET');
which gives you the profile for the user associated with the access_token you are using.
Note that you will only ever get information that you have the permissions for, so if you haven't added a particular bit of info beyond the basics to your apps scope, you will not get it.

Facebook PHP SDK: Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user

I am using the PHP SDK to handle my Facebook connects. Everything was working fine for some time but my script suddenly stopped working. Here is what happens:
$facebook = new Facebook(array('appId' => $app_id, 'secret' => $app_secret, 'cookie' => true));
$user = $facebook->getUser();
if ($user) {
$permissions = $facebook->api('/me/permissions');
} else {
$params = array();
$params['scope'] = 'publish_actions,user_photos';
header('Location: ' . $facebook->getLoginUrl($params));
exit();
}
Returns: Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
The error is caused by the line
$permissions = $facebook->api('/me/permissions');
Any suggestions?
Always check exceptions! Here's a quick example: https://developers.facebook.com/docs/reference/php/facebook-api/
$user returns a user ID for the current user, however, that doesn't guarantee that the user didn't log out meanwhile or that his token is still active. So instead of just relying on this because you got a user_id, check for exceptions. You can either ask the user to click Login again or simply redirect the user to the login page automatically if an exception occurs.
why don't you try
$permissions = $facebook->api(
'/me/permissions ',
'GET',
array(
'access_token' => $_SESSION['active']['access_token']
)
);

Facebook Cant get /me/likes data for part of users

This is the simplest application that can be made:
<?php
include_once "inc/fb/facebook.php";
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'yyy'
));
$user = $facebook->getUser();
var_dump( $facebook->api("/me/likes/") );
?>`
In return I should get an array of pages, that user have already liked. The problem is, that it doesnt work for some users.
Somehow in a process, I have found that error:
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in (...)/fb/base_facebook.php on line 1039
I have tryed to force the get token with:
$access_token = $facebook->getAccessToken();
and it gests the token for every user, but still no table about what user likes.
Any help would be appreciated.
The problem was quite simple:
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream,user_about_me',
'redirect_uri' => $mySettings->app_url,
));
echo "<script> top.location.href='" . $loginUrl . "'</script>";
I have forgoten about user_likes permission in scope. But what is strange to me - why did it worked for most of the users?