dongqi6486 2014-09-15 01:27
浏览 546
已采纳

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.

展开全部

  • 写回答

1条回答 默认 最新

  • dongsuo9982 2014-09-15 02:21
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部