Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 341
Cornelia Secelean Avatar asked May 10 '13 22:05

Cornelia Secelean


People also ask

What is access token used for?

Access tokens are the thing that applications use to make API requests on behalf of a user. The access token represents the authorization of a specific application to access specific parts of a user's data.

How do I enable my personal access token?

From your home page, open user settings and select Personal access tokens. Select + New Token. Name your token, select the organization where you want to use the token, and then set your token to automatically expire after a set number of days. Select the scopes for this token to authorize for your specific tasks.

How do I get an access token response?

OAuth: The Videocourse For Dummies If the token access request is invalid or unauthorized, then the authorization server returns an error response. The access token is given by the authorization server when it accepts the client ID, client password and authorization code sent by the client application.

What is an access token OAuth?

An OAuth Access Token is a string that the OAuth client uses to make requests to the resource server. Access tokens do not have to be in any particular format, and in practice, various OAuth servers have chosen many different formats for their access tokens.


2 Answers

you need to check if already login if not redirect the user to login link

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '191149314281714',
  'secret' => '73b67bf1c825fa47efae70a46c18906b',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
   // $user_profile = $facebook->api('/me');
   $friends = idx($facebook->api('/me/friends'), 'data', array());

  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array('scope'=>'offline_access'));
}
like image 147
Bahaa Odeh Avatar answered Oct 17 '22 13:10

Bahaa Odeh


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.

like image 29
JayNCoke Avatar answered Oct 17 '22 14:10

JayNCoke