简体   繁体   中英

Facebook PHP SDK not getting valid Access Token

I have been following along with the instructions in the developers docs and have run into a problem when for the PHP-SDK tutorials.

https://developers.facebook.com/docs/reference/php/facebook-api/

The "Get the User's profile via the Graph API and print their name" example at the above URL seems to break during the Try, as near as I can tell it's receiving a bad Access Token.

Error Message: Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in myURL/sdk/base_facebook.php on line 1254

I can get a valid access token through FQL queries successfully, but am only getting error messages when using the PHP SDK command $facebook->api('/me','GET') (as the docs and tutorial show) and from what I can tell it should be getting a valid access token on the fly but isn't.

Is there something that I'm missing from somewhere else that I'm meant to be using in this example? I'm not sure what I'm doing incorrectly.

[EDITED TO SHOW CODE]

<?php
  // Remember to copy files from the SDK's src/ directory to a
  // directory in your application on the server, such as php-sdk/
  require_once('php-sdk/facebook.php');

  $config = array(
    'appId' => 'MY_APP_ID',
    'secret' => 'MY_APP_SECRET',
  );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();
?>
<html>
  <head></head>
  <body>

  <?php
    if($user_id) {

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {

        $user_profile = $facebook->api('/me','GET');
        echo "Name: " . $user_profile['name'];

      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {

      // No user, print a link for the user to login
      $login_url = $facebook->getLoginUrl();
      echo 'Please <a href="' . $login_url . '">login.</a>';

    }

  ?>

  </body>
</html>

Alright never mind, for some odd reason, after no changes for 24 hours, this script is working. Thanks for all the help any way folks :)

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