Facebook php sdk setExtendedAccessToken

时间:2013-05-23 11:14:45

标签: php facebook facebook-php-sdk

$facebook = new Facebook(array(
    'appId'  => 'APP_ID',
    'secret' => 'APP_SECRET'
));

$facebook_uid = $facebook->getUser(); 
if (!empty($facebook_uid)) {
    try {
            $facebook_uid = $facebook->getUser();
            $facebook->setExtendedAccessToken();
            echo $accessToken = $facebook->getAccessToken(); die;
        } catch (Exception $e) {
    echo $e;
    die;
}

在执行此代码后对用户进行身份验证后,它会提供扩展访问令牌,但是当我刷新它时会提供应用令牌。

我试图找出问题,并在setExtendedAccessToken()函数

中得到了这个
$this->destroySession();

$this->setPersistentData(
'access_token', $response_params['access_token']
);

2 个答案:

答案 0 :(得分:1)

获得长寿令(https://developers.facebook.com/docs/facebook-login/access-tokens/#extending):

    <?php
    require 'src/facebook.php';  //<--Get it here: https://developers.facebook.com/docs/php/gettingstarted/
    $appID = "YOUR APP ID";
    $appSECRET = "YOUR APP SECRET";
    $facebook = new Facebook(array(
      'appId'  => $appID,
      'secret' => $appSECRET,
      'fileUpload' => false, // optional
      'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
    ));

    $user_id = $facebook->getUser();
    $login_url = $facebook->getLoginUrl();

    ?>

    <html>
    <head></head>
    <body>

    <?php
      if ($user_id){
        $token = $facebook->getAccessToken();
        $path = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=".$appID."&client_secret=".$appSECRET."&fb_exchange_token=".$token;
        //$ret = $facebook->api($path, $method, $params);
        $ch = curl_init();
        // Set some options - we are passing in a useragent too here
        curl_setopt_array($ch, array(CURLOPT_URL => $path));
        // Makes curl_exec() return server response
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // Send the request & save response to $resp
        $resp = curl_exec($ch); 
        // Close request to clear up some resources
        curl_close($ch);
        echo '<div>Long-lived (2 month) '.$resp.'</div>';
        echo 'To debug it, copy to clipboard and click <a href="https://developers.facebook.com/tools/debug/accesstoken/">here.</a>';
      } else{
        echo 'Please <a href="'.$login_url.'">login to get a long-lived access token for facebook api.</a>';
      }

    ?>

    </body>
    </html>

答案 1 :(得分:0)

将LongLivedAccessToken保存到您的数据库或会话中,并在您想要提取Facebook数据时使用此

我使用的代码是:

$session = new FacebookSession($session->getToken());
$longLivedAccessToken = $session->getLongLivedSession($facebook_app_id, $facebook_api_secret);
$longLivedAccessToken=$session->getToken();
//Store longLivedAccessToken in database

我正在使用使用facebook-php-sdk-v4-4.0-dev,他们在docs文件夹中也有文档。 FacebookRedirectLoginHelper.fbmd可能会帮助你...