An active access token must be used to query information about the current user

后端 未结 2 581
别跟我提以往
别跟我提以往 2020-12-29 09:00

I keep getting the error in the title, while using a simple code:

require_once(\'sdk/src/facebook.php\');
require_once(\"AppInfo.php\");
function idx(array $         


        
相关标签:
2条回答
  • 2020-12-29 09:30

    You need to set the access token to be able to get data or act on behalf of the user. Are you going through a login flow to allow authorization to your Facebook application?

    Try the example at https://developers.facebook.com/docs/reference/php/facebook-api/

    It provides the very basic login flow that will grant access to the data you're seeking.

    0 讨论(0)
  • 2020-12-29 09:36

    you need to check if already login if not redirect the user to login link

    require '../src/facebook.php';
    
    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
      'appId'  => '191149314281714',
      'secret' => '73b67bf1c825fa47efae70a46c18906b',
    ));
    
    // Get User ID
    $user = $facebook->getUser();
    
    // We may or may not have this data based on whether the user is logged in.
    //
    // If we have a $user id here, it means we know the user is logged into
    // Facebook, but we don't know if the access token is valid. An access
    // token is invalid if the user logged out of Facebook.
    
    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
       // $user_profile = $facebook->api('/me');
       $friends = idx($facebook->api('/me/friends'), 'data', array());
    
      } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
      }
    }
    
    // Login or logout url will be needed depending on current user state.
    if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
      $loginUrl = $facebook->getLoginUrl(array('scope'=>'offline_access'));
    }
    
    0 讨论(0)
提交回复
热议问题