简体   繁体   English

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

[英]Facebook PHP SDK - how to get a long lived page access token

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? 如何使用常规的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. 弄清楚了,然后@luschn,您博客上的代码似乎存在相同的错误。

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');

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

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