Search code examples
phpfacebook-pagefacebook-access-token

Unknown error when trying to get long lived access token


I am trying to get a long lived access token for one of my pages, using this code:

$page_info = $facebook->api("/page-id?fields=access_token");

$args = array(
    "client_id" => $facebook->getAppId(),
    "client_secret" => $facebook->getAppSecret(),
    "grant_type" => "fb_exchange_token",
    "fb_exchange_token" => $page_info["access_token"]
);

$result = $facebook->api("/oauth/access_token", $args);
echo json_encode($result);

... but I get this response: {"error_code":1,"error_msg":"An unknown error occurred"}

I get the same response if I browse to

https://graph.facebook.com/oauth/access_token?             
    client_id=APP_ID&
    client_secret=APP_SECRET&
    grant_type=fb_exchange_token&
    fb_exchange_token=EXISTING_ACCESS_TOKEN 

as stated in https://developers.facebook.com/roadmap/offline-access-removal/#page_access_token


Solution

  • If I'm reading your code correctly you're passing the Page's access_token to that endpoint, which isn't what the documentation says - you need to exchange the short-lived access_token you have for the Page Admin for a long-lived token, and use that long-lived token to get the Page access_token