Php – An active access token must be used to query information about the current user

facebookfacebook-graph-apiPHP

I keep getting the error in the title, while using a simple code:

require_once('sdk/src/facebook.php');
require_once("AppInfo.php");
function idx(array $array, $key, $default = null) {
  return array_key_exists($key, $array) ? $array[$key] : $default;
}
function he($str) {
  return htmlentities($str, ENT_QUOTES, "UTF-8");
}
$facebook = new Facebook(array(
  'appId'  => AppInfo::appID(),
  'secret' => AppInfo::appSecret(),
  'sharedSession' => true,
  'trustForwarded' => true,
));

$arrayForJSON=array();
$user = $facebook->getUser();
$friends = idx($facebook->api('/me/friends'), 'data', array());
if ($friends) {
  $arrayForJSON['friends']=$friends;
}
print_r($friends);

I really don't know how to solve it, I've been searching and trying many answers on stackoverflow

Best Answer

You need to set the access token to be able to get data or act on behalf of the user. Are you going through a login flow to allow authorization to your Facebook application?

Try the example at https://developers.facebook.com/docs/reference/php/facebook-api/

It provides the very basic login flow that will grant access to the data you're seeking.

Related Topic