Facebook PHP SDK - 如何获取长期存在的页面访问令牌

Having this code, which works:

        $appsecretProof = hash_hmac('sha256', $shortLivedToken, $secret);

        //init curl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php-3.2');

        //get extended user access token
        $url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token' .
            '&client_id=' . $appId .
            '&client_secret=' . $secret .
            '&fb_exchange_token=' . $shortLivedToken .
            '&appsecret_proof=' . $appsecretProof;
        curl_setopt($ch, CURLOPT_URL, $url);
        $curlResult = curl_exec($ch);
        $response_params = [];
        parse_str($curlResult, $response_params);
        $extendedUserToken = $response_params['access_token'];

        $appsecretProof = hash_hmac('sha256', $extendedUserToken, $secret);
        //get extended page access token
        $url = 'https://graph.facebook.com/' . $pageId .
            '?fields=access_token' .
            '&access_token=' . $extendedUserToken .
            '&appsecret_proof=' . $appsecretProof;
        curl_setopt($ch, CURLOPT_URL, $url);
        $curlResult = curl_exec($ch);
        curl_close($ch);
        $pageToken = json_decode($curlResult)->access_token;

How to achieve the same using the regular Facebook SDK? Googling didn't really help. Either I find nothing usable or the solutions do not work, throwing errors like "An access token is required to request this resource" or similar stuff. I already tried a lot but nothing really works.

My last approach was:

        FacebookSession::setDefaultApplication($appId, $secret);

        $shortLivedSession = new FacebookSession($shortLivedToken);
        $shortLivedSession->getLongLivedSession();

        $request = new FacebookRequest($shortLivedSession, 'GET', '/' . $pageId . '?fields=access_token');
        $response = $request->execute();
        $result = $response->getGraphObject()->asArray();
        $pageToken = $result['access_token'];
        $facebookSession = new FacebookSession($pageToken);

This code always returns a short-lived token.

Figured it out and @luschn, there seems to be the same error in the code on your blog.

What helped was to replace these two lines:

    $shortLivedSession->getLongLivedSession();

    $request = new FacebookRequest($shortLivedSession, 'GET', '/' . $pageId . '?fields=access_token');

with these:

    $longLivedSession = $shortLivedSession->getLongLivedSession();

    $request = new FacebookRequest($longLivedSession, 'GET', '/' . $pageId . '?fields=access_token');