为什么getUser()总是返回0?

时间:2012-07-16 05:48:58

标签: php facebook sdk

这个Facebook PHP SDK让我疯狂!

getUser() always return 0

让我解释一下我的所作所为:

$facebook = new Facebook(array(
              'appId'  => FACEBOOK_APP_ID,
              'secret' => FACEBOOK_SECRET,
            ));
$user = $facebook->getUser();


if ($user === 0)
{
   $loginUrl = $facebook->getLoginUrl(array(
        'scope'        => 'email, publish_stream',
        'redirect_uri' => FB_REDIRECT_URI
   ));
   echo "<script type='text/javascript'>window.location = '$loginUrl';</script>";
   exit();
}

简单代码,等于示例。良好...

我在login.php和complete.php中有这段代码

我从login.php发送请求并将redirect_uri设置为complete.php(我在使用Facebook登录的网站中写的相同...显然是一个完整的网址,如https://www.example.com/complete.php

我真的不明白为什么我还是0! 这是不可能的,我按照LoginURL登录Facebook。

我还通过URL传递了“权限”。

我已在此应用注册,因此我真的不明白此SDK继续返回0的原因。

有人可以帮助我吗?

信息:

我只选择了使用Facebook登录的网站,没有别的......“Facebook上的应用程序”未被选中。

修改

https://www.facebook.com/login.php?api_key=216182291818102&skip_api_login=1&display=page&cancel_url=https%3A%2F%2Fwww.example.com%2Fusers%2Fcomplete%2F%3Ferror_reason%3Duser_denied%26error%3Daccess_denied%26error_description%3DThe%2Buser%2Bdenied%2Byour%2Brequest.%26state%3D720fab495a21639032816d3c688ae87b&fbconnect=1&next=https%3A%2F%2Fwww.facebook.com%2Fdialog%2Fpermissions.request%3F_path%3Dpermissions.request%26app_id%3D216182291818102%26redirect_uri%3Dhttps%253A%252F%252Fwww.example.com%252Fusers%252Fcomplete%252F%26display%3Dpage%26response_type%3Dcode%26state%3D720fab495a21639032816d3c688ae87b%26perms%3Duser_birthday%252Cemail%26fbconnect%3D1%26from_login%3D1%26client_id%3D216182291818102&rcount=1

5 个答案:

答案 0 :(得分:0)

我使用相同的例子,它对我有用。 我的连接是这样的,尝试添加cookie = true并检查,希望对你有用

$facebook = new Facebook(array(
          'appId'  => FACEBOOK_APP_ID,
          'secret' => FACEBOOK_SECRET,
          'cookie' => true,
));

答案 1 :(得分:0)

你从哪里得到这个例子?这来自FB Developer页面(https://developers.facebook.com/docs/authentication/server-side/):

 <?php 

   $app_id = "YOUR_APP_ID";
   $app_secret = "YOUR_APP_SECRET";
   $my_url = "YOUR_URL";

   session_start();
   $code = $_REQUEST["code"];

   if(empty($code)) {
     $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
     $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
       . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
       . $_SESSION['state'];

     echo("<script> top.location.href='" . $dialog_url . "'</script>");
   }

   if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
     $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
       . "&client_secret=" . $app_secret . "&code=" . $code;

     $response = file_get_contents($token_url);
     $params = null;
     parse_str($response, $params);

     $graph_url = "https://graph.facebook.com/me?access_token=" 
       . $params['access_token'];

     $user = json_decode(file_get_contents($graph_url));
     echo("Hello " . $user->name);
   }
   else {
     echo("The state does not match. You may be a victim of CSRF.");
   }

 ?>

答案 2 :(得分:0)

您的代码看起来不错。您应该为facebook数组提供另一个参数cookie=>true。您应该参考以下适用于我的教程。

http://www.9lessons.info/2011/02/login-with-facebook-and-twitter.html

希望它对你有用。

答案 3 :(得分:0)

试试这个

$facebook = new Facebook(array(
          'appId'  => FACEBOOK_APP_ID,
          'secret' => FACEBOOK_SECRET,
          'cookie' => true,
));

首先登录facebook帐户然后执行此代码。

$user=$facebook->getUser();

答案 4 :(得分:0)

现在我发布了APP的配置。

App ID/API Key
xxxxxxxxxxxxxxxxxx // your App ID
App Secret
xxxxxxxxxxxxxxxxxxxxxxx //Your secret key
Site URL
http://localhost/      //your site url
Contact Email
example@example.com   //your email id
Support URL
http://localhost      //your support url

我只提供了这么多信息,而且它非常适合我。