Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Facebook PHP SDK getUser always returning 0?

I'm trying to work with a website that requires some information from a Facebook user, I'm using PHP and JS SDKs.

I have a function in PHP:

public function isLoggedOnFacebook() {     $user = $this->_facebook->getUser();     if ($user) {         return $this->_facebook->api("/$user");     }     return false; } 

On a class that is holding the facebook object from the SDK in $this->_facebook.

Then on a block I do this:

<?php if (!$this->isLoggedOnFacebook()): ?> <div>    <fb:login-button show-faces="true" perms="email" width="500" /> </div> <?php endif ?> 

And the FB JS environment is properly set up (I think) so it works. So the user gets the pop up and authorizes the site.

The problem is even after the app is been authorized by the user $user is always 0, meaning $facebook->getUser() always returns 0, and then lists the faces of users, including the logged user, but if I make it call $facebook->api('/me') or whatever, then it'll throw the invalid token exception.

I've seen this problem, but I haven't seen a solution, I have no idea where the problem is and I run out of ideas.

There's a Website tab on the developers' Facebook page in the apps section, where you can set up your Site URL and your Site Domain, and I'm thinking this are the cause of my problem, but I have no knowledge of exactly what these fields are supposed to contain.

like image 893
fd8s0 Avatar asked Jul 22 '11 12:07

fd8s0


People also ask

How add Facebook SDK in PHP?

The PHP Facebook SDK is very easy to implement and allows access to to facebook graph APIs for developers. a note of it to use in the PHP code. Create new app by clicking Add New App. Enter all the required details like name, email id and click on Create APP ID to get APP ID and APP SECRET to access the Facebook API.

What is Facebook PHP SDK?

The Facebook SDK for PHP provides a native interface to the Graph API and Facebook Login. https://developers.facebook.com/docs/php.


2 Answers

I had the same problem and I figured it out that is because SDK uses the variable $_REQUEST and in my environment is not true that is merged with $_GET, $_POST and $_COOKIE variables.

I think it depends on the PHP version and that is why someone made it work by enabling cookies.

I found this code in base_facebook.php:

protected function getCode() {     if (isset($_REQUEST['code'])) {         if ($this->state !== null &&                 isset($_REQUEST['state']) &&                 $this->state === $_REQUEST['state']) {              // CSRF state has done its job, so clear it             $this->state = null;             $this->clearPersistentData('state');             return $_REQUEST['code'];         } else {             self::errorLog('CSRF state token does not match one provided.');             return false;         }     }      return false; } 

And I modified it as you can see below by creating $server_info variable.

protected function getCode() {     $server_info = array_merge($_GET, $_POST, $_COOKIE);      if (isset($server_info['code'])) {         if ($this->state !== null &&                 isset($server_info['state']) &&                 $this->state === $server_info['state']) {              // CSRF state has done its job, so clear it             $this->state = null;             $this->clearPersistentData('state');             return $server_info['code'];         } else {             self::errorLog('CSRF state token does not match one provided.');             return false;         }     }      return false; } 
like image 140
Norberto Yoan Avatar answered Sep 24 '22 23:09

Norberto Yoan


I ran into similar problem. $facebook->getUser() was returning 0 and sometimes it returned valid user id when user wasn't actually logged in, resulting in Fatal Oauth error when I tried to make graph api calls. I finally solved this problem. I don't know if it is the right way but it works. Here is the code :

<?php include 'includes/php/facebook.php'; $app_id = "APP_ID"; $app_secret = "SECRET_KEY"; $facebook = new Facebook(array(     'appId' => $app_id,     'secret' => $app_secret,     'cookie' => true ));  $user = $facebook->getUser();  if ($user <> '0' && $user <> '') { /*if valid user id i.e. neither 0 nor blank nor null*/ try { // Proceed knowing you have a logged in user who's authenticated. $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { /*sometimes it shows user id even if user in not logged in and it results in Oauth exception. In this case we will set it back to 0.*/ error_log($e); $user = '0'; } } if ($user <> '0' && $user <> '') { /*So now we will have a valid user id with a valid oauth access token and so the code will work fine.*/ echo "UserId : " . $user;  $params = array( 'next' => 'http://www.anujkumar.com' ); echo "<p><a href='". $facebook->getLogoutUrl($params) . "'>Logout</a>";  $user_profile = $facebook->api('/me'); echo "<p>Name : " . $user_profile['name']; echo "<p>"; print_r($user_profile);  } else {/*If user id isn't present just redirect it to login url*/ header("Location:{$facebook->getLoginUrl(array('req_perms' => 'email,offline_access'))}"); }  ?> 
like image 23
anujkk Avatar answered Sep 22 '22 23:09

anujkk