dongqi6486 2014-09-15 09: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 10: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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化