简体   繁体   English

获取Facebook Graph API扩展访问令牌

[英]Get Facebook Graph API extended access token

I can get the posts I need from a Facebook Page using this code in PHP: 我可以使用PHP中的代码从Facebook页面获取我需要的帖子:

$token = "ACCESS_TOKEN";
$data = file_get_contents("https://graph.facebook.com/PAGE_ID/feed?access_token=".$token);
$data = json_decode($data, true);

However, the access token it just last 1 hour. 但是,访问令牌只持续了1个小时。 I saw that it's possible to request a long live token that lasts 60 days, but stills not so clear for me with a good example. 我看到可以申请一个持续60天的长期令牌,但对我来说仍然不太清楚,有一个很好的例子。

Do you guys know a good example using PHP? 你们知道使用PHP的好例子吗? Thank you in advance! 先感谢您!

As per the Facebook documentation , you need to pass the short-lived access-token with app secret and in return you will get the Long-lived access-token 根据Facebook文档 ,您需要使用app secret传递短期访问令牌,作为回报,您将获得长期访问令牌

To get the long-lived user access_token simply pass your own client_id (your app_id), your app_secret, and the non-expired, short-lived access_token to the endpoint below. 要获取长期存在的用户access_token,只需将您自己的client_id(您的app_id),您的app_secret以及未过期的短期access_token传递给下面的端点。 You will be returned a new long-lived user access_token; 您将返回一个新的长期用户access_token; this access_token will exist in addition to the short-lived access_token that was passed into the endpoint. 除了传递到端点的短期access_token之外,此access_token还将存在。

Code Sample 代码示例

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 

You should use like that $accessToken = $session->getAccessToken(); $longLivedAccessToken = $accessToken->extend(); 你应该使用$accessToken = $session->getAccessToken(); $longLivedAccessToken = $accessToken->extend(); $accessToken = $session->getAccessToken(); $longLivedAccessToken = $accessToken->extend();

then check access token with url. 然后用url检查访问令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM