Facebook API登录错误以及如何设置访问令牌

时间:2013-06-19 05:05:25

标签: php facebook

Facebook API对我不起作用..当我评论该行

$naitik = $facebook->api('/naitik');

抛出错误

  

“参数app_id是必需的”

其他

"Fatal error: Uncaught OAuthException: Invalid OAuth access token signature. thrown in /mounted-storage/home122a/sub004/sc44038-VQQX/psychegames.com/test/src/base_facebook.php on line 1271"  

我的PHP代码是

   <?php

 require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
 $appid = 'MYAPPID',
  $appsecret = 'MYAPPSECRETID',
  $pageId = 'MYPAGEID'
));

$access_token = $facebook->getAccessToken();

// 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');
 } 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(
'req_perms' => 'publish_stream,read_friendlists'
  ));
 }

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>

3 个答案:

答案 0 :(得分:0)

$ facebook-&gt; api('/ me')用于获取当前登录用户的facebook ID。一旦您评论该行,API无法对用户进行身份验证,因此抛出错误。但是你为什么评论那条线?

答案 1 :(得分:0)

存在大写错误。

$appid = 'MYAPPID',

应该是

$appId = 'MYAPPID',

并且

$appsecret = 'MYAPPSECRETID',

应该是

$secret = 'MYAPPSECRETID',

可以在https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php

找到它应如何工作的示例

答案 2 :(得分:0)

你应该替换

// This call will always work since we are fetching public data. Replace it with $naitik = $facebook->api('/naitik');

// This one.. $userData = $facebook->api('/me?fields=id,name,email,friends,last_name,gender,hometown,birthday,picture,bio,timezone');

这将帮助您获得授予权限的用户数据。

适用于Facebook SDK for PHP - API参考(v3.2.3)和php(v5.3)。