-1

以下控制檯日誌「必須使用活動訪問令牌查詢有關當前用戶的信息。」 ?顯然,我的JS是沒有得到從PHP訪問令牌 - 我該如何糾正這種下面的代碼是什麼FB貼在自己的博客虛擬克隆: https://developers.facebook.com/blog/post/534/Facebook js sdk「/ me/permissions」返回無訪問令牌(js sdk/php sdk衝突?)

<?php 
$basePath = "/usr/lib/fb"; 

// Include the fb sdk 
require 'fb/src/facebook.php'; 

// Create our Application instance 
$facebook = new Facebook(array(
    'appId' => '#####', // I do repeat this in fb.js so I can externalize it 
    'secret' => '#####', 
)); 

// See if there is a user from a cookie 
$user = $facebook->getUser(); 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $user_profile = $facebook->api('/me'); 
    } catch (FacebookApiException $e) { 
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; 
    $user = null; 
    } 
} 

?> 
<!DOCTYPE html> 
<html xmlns:fb="http://www.facebook.com/2008/fbml"> 
    <body> 
    <?php if ($user_profile) { ?> 
     Your user profile is 
     <pre>    
     <?php print htmlspecialchars(print_r($user_profile, true)) ?> 
     </pre> 
    <?php } else { ?> 
     <fb:login-button></fb:login-button> 
    <?php } ?> 
    <div id="fb-root"></div> 
    <script>    
     window.fbAsyncInit = function() { 
     FB.init({ 
      appId: '<?php echo $facebook->getAppID() ?>', 
      cookie: true, 
      xfbml: true, 
      oauth: true 
     }); 
     FB.Event.subscribe('auth.login', function(response) { 
      window.location.reload(); 
     }); 
     FB.Event.subscribe('auth.logout', function(response) { 
      window.location.reload(); 
     }); 

     FB.api('/me/permissions', function (response) { 
      console.log(response); 
     }); 

     }; 


     (function() { 
     var e = document.createElement('script'); e.async = true; 
     e.src = document.location.protocol + 
      '//connect.facebook.net/en_US/all.js'; 
     document.getElementById('fb-root').appendChild(e); 
     }()); 
    </script> 
    </body> 
</html> 

回答

1

您加載和啓動FB SDK ,但你需要認證用戶。
對於JS驗證單獨您需要使用FB.login method,但如果你的php負責認證用戶,那麼你可以叫FB.getLoginStatus,如:

window.fbAsyncInit = function() { 
    FB.init({ 
     appId: '<?php echo $facebook->getAppID() ?>', 
     cookie: true, 
     xfbml: true, 
     oauth: true 
    }); 
    FB.Event.subscribe('auth.login', function(response) { 
     window.location.reload(); 
    }); 
    FB.Event.subscribe('auth.logout', function(response) { 
     window.location.reload(); 
    }); 

    FB.getLoginStatus(function(response1) { 
     if (response1.status === 'connected') { 
      FB.api('/me/permissions', function(response2) { 
       console.log(response2); 
      }); 
     } 
     else { 
      // user is not logged in or not authorized with the app. 
      // use a login button or FB.login 
     } 
    }); 
}; 
0

要調用me/permissions甚至當用戶處理不當授權您應用程序,

而是執行此操作,

<?php if ($user_profile): ?> 
    FB.api('/me/permissions', function (response) { 
     console.log(response); 
    }); 
<?php endif; ?>