简体   繁体   中英

An active access token must be used to query information about the current user. Facebook PHP SDK v4

I'm passing the facebook login token from ember-cli to my laravel server to register/login my user but I'm stuck with this error.

{"error":{"type":"Facebook\\FacebookAuthorizationException","message":"An active access token must be used to query information about the current user.","file":"C:\\xampp\\htdocs\\LoginApp\\vendor\\facebook\\php-sdk-v4\\src\\Facebook\\FacebookRequestException.php","line":134}}

Here is my function

public function authorizeFacebookUser() {
        $facebook_token = Input::get('facebook_access_token');

        FacebookSession::setDefaultApplication('xxxxxxxx', 'xxxxx');

        $session = new FacebookSession((string) $facebook_token);

        if ($session) {
            // have session
            try {
                $user_profile = (new FacebookRequest(
                    $session, 'GET', '/me'
                ))->execute()->getGraphObject(GraphUser::className());

                echo "Name: " . $user_profile->getName();

            } catch(FacebookRequestException $e) {

                echo "Exception occured, code: " . $e->getCode();
                echo " with message: " . $e->getMessage();

            }
        }
    }

After creating a session with the access_token, you should validate it to make sure it's correct and working:

  try {
    if ( !$session->validate() ) {
      $session = null;
    }
  } catch ( Exception $e ) {
    // catch any exceptions
    $session = null;
  }

Then you can make the API call to get the user's profile information. Hopefully, the above code will display any errors related to the access_token.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM