繁体   English   中英

如何使用 PHP 更新/扩展 facebook 访问令牌?

[英]How to renew/extend facebook access tokens with PHP?

Facebook 已删除 offline_access 令牌功能,现在每当用户访问您的网站时都必须更新令牌以保持其活动状态。

假设有人已经授予您网站访问权限,并且您为他们存储了一个令牌。 您将使用什么代码与 Facebook 的 PHP 库一起更新该令牌?

您可以通过以下方式扩展您的令牌:

原场景

  • 您的应用向用户请求权限
  • 您提示用户登录/授予权限
  • 您获得用户的令牌(短期令牌)并使用 grant_type=fb_exchange_token 通过 CURL 或其他方式交换 60 天的令牌
  • 你坚持令牌

现在您拥有该令牌,可以在长达 60 天内用它做您想做的事。 最多,因为用户可以更改密码,取消对应用程序的授权等,并且令牌将变得无效。 每次用户访问您的页面时,您可以做些什么来延长令牌,您可以检查他们是否通过 javascript 登录,如果是,请拨打 ajax 调用您的服务器以将现有令牌延长 60 天今天。 您可以拨打任意多个电话,只有第一个电话有效。 我是这样做的:

  1. 在加载事件期间的某处页面上,添加如下内容:

     FB.getLoginStatus(function (response) { if (response.status === 'connected') { $.ajax({ type: "POST", async: false, url: YOUR_URL, dataType: "text", data: {token: response.authResponse.accessToken } }); } }); //rest of jquery ajax call here

这将为用户获取一个新的客户端访问令牌并将其发送到服务器

  1. 服务器然后可以获取该令牌并将其交换为 60 天的令牌

    $token_url = "https://graph.facebook.com/oauth/access_token?client_id=".FACEBOOK_CLIENT_ID."&client_secret=".FACEBOOK_SECRET."&grant_type=fb_exchange_token&fb_exchange_token=".$token; $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($c, CURLOPT_URL, $token_url); $contents = curl_exec($c); $err = curl_getinfo($c,CURLINFO_HTTP_CODE); curl_close($c); $paramsfb = null; parse_str($contents, $paramsfb);

参考:

https://developers.facebook.com/roadmap/offline-access-removal/

如果用户在 60 天内返回您的网站,那只会延长令牌。 如果没有,您将需要再次提示权限。

更新

是的@zerkms 是对的,如果应用程序有权限则不需要 access_token。

With this permission, you can publish content to a user's feed at any time. However, please note that Facebook recommends a user-initiated sharing model. Please read the Platform Policies to ensure you understand how to properly use this permission. Note, you do not need to request the publish_stream permission in order to use the Feed Dialog, the Requests Dialog or the Send Dialog.

所有扩展权限都具有类似的权限: https://developers.facebook.com/docs/authentication/permissions/

这是我目前正在做的

public function setExtendAccessToken($accessToken = NULL) {

enter code here
    if(!$accessToken) return;

    $graphUrl = 'https://graph.facebook.com/oauth/access_token?client_id='.$facebookAppId.
                '&client_secret='.$facebookSecret.
                '&grant_type=fb_exchange_token&fb_exchange_token='.$accessToken;
    $accessToken = @file_get_contents($graphUrl);
    parse_str($accessToken); //get the access_token param in the string and would be named $access_token
    if(!$access_token) $access_token = $accessToken; //if cannot be extended then just return the access token with 2 hours expiry
    return $access_token;
}
use Facebook\FacebookSession;
use Facebook\GraphSessionInfo;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

    FacebookSession::setDefaultApplication('YOURAPPID', 'SECRET');

    $user_accessToken = $_COOKIE['access_token_facebook']

    $session = new FacebookSession($user_accessToken);

    try {
        $session->validate();
    } catch (FacebookRequestException $ex) {
        // When Facebook returns an error
        echo $ex->getMessage();
    } catch (\Exception $ex) {
        // When validation fails or other local issues
        echo $ex->getMessage();
    }
    if ($session) {
        // Exchange token for long token
        $longToken = $session->getExchangeToken();
        // ... your other stuff
    }

参考: https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens https://developers.facebook.com/docs/facebook-login/access-tokens#extending

暂无
暂无

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

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