How to get user access token?

How to get user access token?

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

Stefan Bogdanescu

Founder & Senior Architect

How to get user access token?

When attempting to fetch specific user data via the Facebook Graph API, such as a list of scores from friends, you will often encounter a FacebookApiException stating that a user access token is required. This error occurs because certain resources are strictly scoped to individual users rather than applications. An app access token grants read-only access to public data and allows you to impersonate the app in some contexts, but it cannot retrieve private information tied to a specific user profile or their friends list without explicit permission.

The distinction between an app access token and a user access token is fundamental to Facebook's security model. The app token acts as your application's identity, while the user token represents the permissions granted by a specific human account. You can think of the app token as the keys to the building (public areas), whereas the user token is the key card required to enter an employee's specific office. To request data like /me/scores or any other node containing private information, you must possess the latter.

Obtaining a valid user access token requires a direct interaction between your application and the user attempting to log in. You cannot generate this token server-side without user involvement. The standard approach involves redirecting the user to Facebook's OAuth authorization URL. This URL includes parameters for the client_id, the specific permissions you need (such as public_profile or user_likes), and a unique state parameter to prevent CSRF attacks.

$redirectUri = 'https://www.facebook.com/dialog/oauth';
$params = [
    'client_id' => config('services.facebook.client_id'),
    'redirect_uri' => config('services.facebook.redirect'),
    'scope' => 'public_profile,email,user_friends', // Specific permissions required
    'state' => 'random-state-value'
];

$url = $redirectUri . '?' . http_build_query($params);

Once the user authorizes your app on the Facebook login screen, they are redirected back to your redirect_uri with a code parameter appended. This code is temporary and must be exchanged for an access token via a server-side request to the Facebook Token Exchange endpoint. It is critical that you do not attempt this exchange from client-side JavaScript, as exposing the resulting long-lived access token would compromise user security.

use Facebook\Facebook;

$fb = new Facebook([
    'app_id' => config('services.facebook.app_id'),
    'default_graph_version' => 'v18.0',
]);

$helper = $fb->getRedirectLoginHelper();
$fetchedToken = $helper->getAccessToken($code);

if ($fetchedToken) {
    // Token exchange happens here to get a valid user access token
}

The resulting token obtained from the exchange is what you use in subsequent API calls. This token contains the necessary scopes defined by the user during authorization. If you attempt to call /me/scores with this token, the request will succeed provided the read_stream or similar permissions required by that specific endpoint are included in the scope list.

It is also important to note that tokens have expiration times. Short-lived tokens last for approximately two hours, while long-lived tokens can last for 60 days. Your application logic must handle token refreshes automatically using the refresh token mechanism if you choose to implement it, ensuring a seamless experience for the user even after their initial session expires. Failing to manage this lifecycle often leads to sudden authentication failures in production environments.

For developers working within the Laravel ecosystem, handling these OAuth flows is common when integrating third-party services. While Facebook provides comprehensive documentation on permissions and token exchange at https://developers.facebook.com/docs/reference/api/permissions/, managing these tokens securely requires careful implementation of redirects and server-side validation to avoid exposing sensitive credentials.

Our network