Introduction
In this article, I’m going to share how to get Facebook’s long lived token and never expired token via either PHP or Facebook’s Graph API Explorer.
Currently I’m working on a Facebook live-stream selling optimized system, and found that the short lived token for web is only effective for not longer than 2 hours, far shorter than 3 months on Android, and 2 months on iOS.
Although it will be okay if we just set our script properly, I spent some time figuring out how to get long lived token and never expired token
Facebook Graph API Explorer
long lived token
Firstly, let’s get long lived token via Facebook’s Graph API Explorer
Let’s create some Test Users
Get token from Test Users
On Graph API Explorer
Enter the token we just got
Press submit
Press the exclamation mark beside token,and press Open In Access Token Tool
Click Extend Access Token on bottom left corner
got long lived token
Never expired token
Now, let’s get never expired token through Graph API Tool
- Firstly, let’s login with Test User account, and create a fan page
- Secondly, let’s repeat all the process mentioned above, and then we will get a never expired token
PHP
long lived token
Now let’s get long lived token via calling Facebook’s API with PHP
- Call Facebook’s API with PHP’s function
file_get_contents
public static function getLongLivedToken($token)
{
$url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=yourClientID&client_secret=yourClientSecret&fb_exchange_token=shortLivedToken;
return json_decode(file_get_contents($url), true);
} - You will get information below:
{
"result": true,
"response": {
"access_token": "EAAEpKfFACZA8BAGyTFU29VFIlEjhDaUe66eliyWdGQDfVTBUUdFZBZAGeZBEgTEwxgthvdABuzECYi1ahqm8ZCYNRSV9YMnegq7XxCouP1sR8kXMdnNFysGb2IHZBhSB3KENeTZCBzHrFSJ9BJLt9k6xkuWkJsVHnG0KahmFmybKTG6pVaFoZATN",
"expires_in": 5182393
}
}
Conclusion
As to how to get never expired token via PHP, it seems like we will need to pass the APP review and get more permission, so I’m not able to test it for now.
Comments