1
$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']
);
4

2 回答 2

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>
于 2013-11-24T07:11:54.920 回答
0

Save the LongLivedAccessToken to your database or session and use this when you want to pull facebook data

The code that I use is:

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

I am using Use facebook-php-sdk-v4-4.0-dev and they have docs too in docs folder. FacebookRedirectLoginHelper.fbmd might help you...

于 2015-01-12T11:50:17.630 回答