My question: how to get long-lived access token from PHP API. I have read through all the prior posts about this matter and still don't have the answer. Basically, I get a short-lived access token from the API explorer. Subsquently, I wrote a simple PHP prohgram invoking the facebook graph API to request a long-lived access token. Yet, it isn't working. I am missing something here. Here is the snippet of the PHP code that I wrote:
$response = $fb->request("GET", "GET /oauth/access_token?
grant_type=fb_exchange_token&
client_id={my app id}&
client_secret={my app secret}&
fb_exchange_token={'the short-lived token obtained from the api
explorer'}")
..//error checking...
//
// then I call this:
$token = $response->getaccessToken();
// end of program
It turns out the getaccessToken() return the same short-lived token that I passed to the API. So, I am running of idea of how to make it work.
According to the API doc, the call with the input parameter of "fb_exhcnage_token" is supposed to return a "long-lived access token". Yet, I am not getting it.
This is valid in 2019, after long-term tokens became 60-day expiries. Here are Facebook's instructions to swap a short-term token (provided in front-end) for a long-term token (server only):
https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing/
Generate a Long-lived User or Page Access Token
You will need the following:
A valid User or Page Access Token
Your App ID
Your App Secret
Query the GET oath/access_token endpoint.
curl -i -X GET "https://graph.facebook.com/{graph-api-version}/oauth/access_token?
grant_type=fb_exchange_token
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={your-access-token}"
Sample Response
{
"access_token":"{long-lived-access-token}",
"token_type": "bearer",
"expires_in": 5183944 //The number of seconds until the token expires
}
You can make this curl request in PHP using curl_init()
:
https://www.php.net/manual/en/book.curl.php
Or if you would rather use the Facebook PHP SDK, you can do this:
1.) Create a FB request object with your credentials. 2.) Request long-term token using the FB object and the access token
public function createFacebookRequestObject($facebookUser)
{
try {
return new Facebook([
'app_id' => config('env.FACEBOOK_GRAPH_API_ID'),
'app_secret' => config('env.FACEBOOK_GRAPH_API_SECRET'),
'default_graph_version' => 'v4.0',
'default_access_token' => $facebookUser->access_token
]);
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
return 'Facebook SDK returned an error: ' . $e->getMessage();
}
}
public function fetchLongTermAccessToken($fb, $facebookUser)
{
return $fb->get(
"/oauth/access_token?grant_type=
fb_exchange_token&fb_exchange_token="
. $facebookUser['access_token']
. '&client_id='
. config('env.FACEBOOK_APP_ID'), );
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.