1

This Facebook PHP SDK is driving me CRAZY!

getUser() always return 0

let me explain what I do:

$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();
}

simple code, equals to the example. Good...

I have this code in login.php and complete.php

I send the request from login.php and set the redirect_uri to complete.php (the same I wrote in Website with Facebook Login...obviously a complete URL like https://www.example.com/complete.php)

I really do not understand Why I still get 0! It is not possible, Im logged in Facebook following the LoginURL.

I also passed the "permissions" with the URL.

I already registered at this app, so I really do not understand the reason why this SDK is continuing to return 0.

Could someone help me?

INFO:

I only have selected Website with Facebook Login, nothing else... "App on Facebook" is not selected.

EDIT:

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
Dail
  • 4,622
  • 16
  • 74
  • 109
  • In which browser are you testing? Do you have **crt** file on the same folder of the SDK? the appId and secret belong to the app and URL you set on the config of the app? – Jose Adrian Jul 16 '12 at 05:54
  • @JoseAdrian no one... I only selected "Website with Facebook login". As I told in the redirecT_uri I have set mine page (on my server, like: https://www.example.com/complete.php). I have tested ii in Chrome and IE 8-9. I'm not copied the code, I have wrote it myself. APPID and SECRET are correct. – Dail Jul 16 '12 at 05:58
  • What's your Facebook Login URL? (use example as the domain). Because I think it should be like that domain/ or domain/index.php (if your are using CI, etc). or domain/my_app/. Also, try changing your FB_REDIRECT_URI for the same login.php. And change your Facebook Login URL to that one. – Jose Adrian Jul 16 '12 at 06:02
  • @JoseAdrian take a look at the edit – Dail Jul 16 '12 at 06:15
  • @JoseAdrian the real pages are www.example.com/users/facebook/ that redirects to www.example.com/users/complete/ (redirect_uri) – Dail Jul 16 '12 at 06:16
  • Only keep the domain on the Facebook Login URL with a slash at the end and erase everything else. http://mydomain.com/ – Jose Adrian Jul 16 '12 at 06:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13921/discussion-between-jose-adrian-and-dail) – Jose Adrian Jul 16 '12 at 06:19
  • @JoseAdrian I'm in chat.... I have done the change, I try in few minutes. – Dail Jul 16 '12 at 06:24

5 Answers5

0

i am used same example and it's work for me. my connection is like this,try to add cookie = true and check, hope will work for you

$facebook = new Facebook(array(
          'appId'  => FACEBOOK_APP_ID,
          'secret' => FACEBOOK_SECRET,
          'cookie' => true,
));
prakash
  • 184
  • 2
  • 10
  • please check your appId and secret key, it's proper or not – prakash Jul 16 '12 at 06:01
  • unfortunately I also tried 'cookie' => true but nothing happen. Please Could you post in some way the configuration you have in https://developers.facebook.com/apps (I think the problem is there because the code seems correct). Thanks – Dail Jul 16 '12 at 06:03
  • do one thing, remove your register app in facebook, and then add again – prakash Jul 16 '12 at 06:31
0

Where did you get the example from? This is from the FB Developer page (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.");
   }

 ?>
Chris
  • 4,204
  • 1
  • 25
  • 30
  • why do i have to use this code when SDK should do all the things? it is too strange... I'm thinking some wrong the it configuration of the app (https://developers.facebook.com/apps) – Dail Jul 16 '12 at 06:18
  • are you using the new sdk from https://github.com/facebook/facebook-php-sdk/ ? From my understanding $facebook->getUser() needs the login cookie from facebook, so check if the cookie is actually present. They also have examples (https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php) – Chris Jul 16 '12 at 20:56
0

Your code is looking good. You should provide one more parameter to facebook array that is cookie=>true. You should refer the following tutorial which worked for me.

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

Hope it will work for you.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
  • I also tested it with 'cookie' => true. It is really too strange. Maybe there are some problems in https://developers.facebook.com/apps Could you also post the configuration you have there ? – Dail Jul 16 '12 at 06:11
  • check your siteURL part in `https://developers.facebook.com/apps` Might be problem is there. – J.K.A. Jul 16 '12 at 06:19
  • that's the same i used in redirect_uri – Dail Jul 16 '12 at 06:22
  • Check your APP configuration once again or create another new APP and test the code with it. – J.K.A. Jul 16 '12 at 06:48
0

Try this

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

and first you login in facebook account then execute this code.

$user=$facebook->getUser();
j0k
  • 22,600
  • 28
  • 79
  • 90
Vishnu Sharma
  • 1,357
  • 12
  • 11
0

Now I am posting the configuration of 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

I only provided this much information and its perfectly working for me.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163