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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

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

When you encounter the error "An active access token must be used to query information about the current user" while executing code against the Facebook Graph API, the issue is rarely a bug in your PHP logic or a misconfigured Facebook SDK instance. The root cause is almost always how the authentication flow handles the lifecycle of the access token versus the session cookie.

In the snippet you provided, the Facebook class is instantiated with sharedSession set to true. This setting tells the SDK to rely on the browser's PHPSESSID and the corresponding fbsr_... (Facebook Session) cookie to identify the user. While convenient for standard web apps where users remain logged into Facebook across sessions, this behavior breaks down as soon as the request is made via a direct HTTP call (like a curl request from a script), a mobile app, or an API endpoint that does not persist the session cookie with the server-side PHP process.

When you call $facebook->getUser(), the SDK attempts to resolve the user ID by checking the active session. If that session is missing or expired on the client side, the SDK cannot identify who "current user" refers to. Consequently, subsequent calls like api('/me/friends') fail because the /me endpoint strictly requires a valid, active access token embedded in the request headers (specifically the Authorization: Bearer <token> header) rather than relying on a server-side session cookie.

To resolve this without forcing every user to stay logged in via a persistent browser session, you must switch from session-based authentication to explicit token-based authentication. This involves retrieving an access token first and then injecting it into your API requests.

Here is how you can refactor the logic to handle both scenarios or force token usage:

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;
}

// Configuration
$facebook = new Facebook(array(
    'appId'  => AppInfo::appID(),
    'secret' => AppInfo::appSecret(),
));

// Step 1: Attempt to get the user using session or token
$userId = $facebook->getUser();

if (!$userId) {
    // No active session/token. You must either redirect for login 
    // or provide a specific access token if this is an API call.
    echo "No active user found. Ensure the request includes a valid access token.";
    exit;
}

// Step 2: Fetch friends using the verified user context
$friends = $facebook->api('/me/friends');

print_r($friends);

However, if your use case involves fetching data for a specific user without maintaining a persistent session cookie (common in server-to-server or API-driven integrations), you must bypass getUser() entirely. Instead, pass the access token directly to the Graph API client or construct the request manually with the Authorization header.

For direct API calls using cURL:

$access_token = AppInfo::getAccessToken(); // Retrieve from config or secure storage
$url = "https://graph.facebook.com/v18.0/me/friends?fields=id,name&limit=25";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer " . $access_token,
    "Content-Type: application/json"
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
print_r($data);

The trade-off here is that you are now responsible for managing the token's expiration and scope. Access tokens granted via OAuth 2.0 have a limited lifespan (typically one hour for short-lived tokens). If you rely on long-lived tokens, you must handle the refresh logic securely on your backend to avoid storing sensitive credentials in plaintext.

Furthermore, ensure that when using sharedSession, the Facebook JavaScript SDK is included on the page making the request so that the session cookie is actually present in the browser environment. If this condition isn't met, falling back to explicit token handling is the only reliable path forward. This approach ensures your application remains robust regardless of the user's login state or the environment through which the request is initiated.

For developers looking for more structured ways to handle these authentication flows within modern PHP ecosystems, platforms like https://laravelcompany.com often provide insights into integrating OAuth2 providers with Laravel's built-in session and token management capabilities, which can abstract away much of the manual header manipulation required here.

Our network