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 に答える