1

Whenever I try to login a prompt opens asking for the basic permissions, after that its being redirected to my redirect_uri with an URL

=">http://localhost/demo/?code=AQDwzia3Wx1BktixF59jVHbm0ViGVJm8Xhb2tNZDyYreZh0KoSJhrSsJ8Aa2KX3gocwR0XNQjQz7ZlBh26_nBi-3iOMByhVO2cxwJ8maC4IHxBacfqXjzqIyBaZQbWKUUxPI6VBrqBgFXQasj7PEtmug7lt93dK4fmMC2A4i2dUYU-gSvzn0f0ZdB3eT_aSvgR1KoLCmQgLh3xix4H05QR6LCP9nLtQC4l9rMJW83kS0PNmWq0COZYvGfuX1R7519Fn3iXRB9F0MTsK1KQ_ulpK84PUCkuMu8et88Lln0ZwuzaPo0oERelkPWYnrrTKa-5w&state=ed66ea618d8076d9e72c15d9a65a6312#=

Even though facebook->getUser() returns 0

Here is my code

<?php
require_once('php-sdk/facebook.php');
$facebook = new Facebook (array(
    'appId' => '1234',
    'secret' => '12313',
    'cookie' => true
    ));
?>

<html>
    <head> <title> Warming Up with FB API </title> </head>
    <body> <h1> Hello World </h1> </body>
</html>
<?php
$loginUrl = $facebook->getLoginUrl(array ( 
        'display' => 'popup',
        'redirect_uri' => 'http://localhost/demo'
        ));

$user = $facebook->getUser();
//echo $user. '</br>';
if ($user) {
    echo '<em>User Id: </em> '.$user;
} else {
    $loginUrl = $facebook->getLoginUrl(array ( 
        'display' => 'popup',
        'redirect_uri' => 'http://localhost/demo'
        ));
    echo '<a href = "'.$loginUrl.'">Login Here </a> ';
 }
?>

I know its a very trivial question but I am kinda stuck at this and unable to proceed further. Kindly suggest what to do.

*UPDATE*

Leaving the App Domain empty solved my problem.

Snehasish
  • 1,051
  • 5
  • 20
  • 37

5 Answers5

11

if someone is still banging their head over this here is what is wrong now. I was hired to fix this mess!

  1. Check App domain in fb panel , must match with the domain where your app is.

  2. edit base_facebook.php find:

    public function getAccessToken() {

    if ($this->accessToken !== null) {
      // we've done this already and cached it.  Just return.
      return $this->accessToken;
    }
    
    // first establish access token to be the application
    // access token, in case we navigate to the /oauth/access_token
    // endpoint, where SOME access token is required.
    $getApplicationAccessToken = $this->getApplicationAccessToken();
    $this->setAccessToken($getApplicationAccessToken);
    $user_access_token = $this->getUserAccessToken();
    if ($user_access_token) {
      $this->setAccessToken($user_access_token);
    }
    return $this->accessToken;
    

    }

to::

  public function getAccessToken() {

    if ($this->accessToken !== null) {
      // we've done this already and cached it.  Just return.
      return $this->accessToken;
    }

    // first establish access token to be the application
    // access token, in case we navigate to the /oauth/access_token
    // endpoint, where SOME access token is required.
    $getApplicationAccessToken = $this->getApplicationAccessToken();
    $this->setAccessToken($getApplicationAccessToken);
    $user_access_token = $this->getUserAccessToken();
    if ($user_access_token) {
      //$this->setAccessToken($user_access_token);
    $this->accessToken = $user_access_token; //edit; msolution
    }
    return $this->accessToken;
  }
  1. Next find the function: getAccessTokenFromCode()

find the line:

parse_str($access_token_response, $response_params); 

replace it with:

//parse_str($access_token_response, $response_params); //edit:: msolution;;
$response_params = json_decode($access_token_response, true );

commenting the original and adding json_decode

thats it!

GeekKing
  • 311
  • 5
  • 11
3

If User is logged in and still getting $user = 0 I faced this issue on different occasions but SDK issue is not happening to everyone. So I'm not sure what goes wrong here. But I dig in to it little bit and found solution as mentioned below.

SOLUTION : This worked for me after trying for many solutions for this issue.

In base_facebook.php file, find the makeRequest() method and check for following Line.

$opts = self::$CURL_OPTS; 

Immediately following it, add this line

$opts[CURLOPT_SSL_VERIFYPEER] = false;  

More details can be found here - http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

2

Change this

$loginUrl = $facebook->getLoginUrl(array ( 
        'display' => 'popup',
        'redirect_uri' => 'http://localhost/demo'
        ));

to

$loginUrl = $facebook->getLoginUrl(array ( 
        'display' => 'popup',
        'redirect_uri' => 'http://localhost/demo/index.php'
        ));

And see if this works !!

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • I tried this too but didn't work. I am getting an URL like http://localhost/demo/?code=xyzxzxz – Snehasish Jan 26 '14 at 19:43
  • Your redirect url must match with the canvas url in the setting, no need to provide the URL like /demo/?code=xyzz – Abhik Chakraborty Jan 26 '14 at 19:47
  • Sorry. Actually the code parameter was too long so I thought it would be better to truncate it. However I corrected the canvas url settings. – Snehasish Jan 26 '14 at 19:51
  • also under Valid OAuth redirect URIs you may need to enter the redirect url in advanced setting – Abhik Chakraborty Jan 26 '14 at 19:52
  • hmm what is the setting for canvas URL and domain ? – Abhik Chakraborty Jan 26 '14 at 19:58
  • Valid OAuth redirect URIs http://localhost/demo http://localhost/demo/index.php Site URL http://localhost/demo/ App Domains localhost – Snehasish Jan 26 '14 at 20:01
  • This localhost issue may happen for various reason try checking these http://stackoverflow.com/questions/13376710/facebook-app-domain-name-when-using-localhost http://stackoverflow.com/questions/2459728/how-to-test-facebook-connect-locally and make the domain name empty in the setting. – Abhik Chakraborty Jan 26 '14 at 20:16
  • I added the platform "Apps on Facebook" and when I visit the Canvas URL everything works fine. User ID gets echoed. So what's wrong with providing a Website URL ?? – Snehasish Jan 26 '14 at 20:17
  • Thanks @Abhik Problem resolved. Leaving Domain Name empty resolved it. – Snehasish Jan 26 '14 at 20:21
1

After several hours, the solution for me was (Let's say my site is http://www.site123.com):

Settings @ https://developers.facebook.com

  1. App Domain: site123.com (even blank it will work though)
  2. Site URL: http://site123.com IMPORTANT : NOT http://www.site123.com

PHP Code - fb_config.php

//Declarations
$app_id     = "{you AppID}";
$app_secret = "{your App Secret}";
$site_url   = "http://site123.com/receive_data_from_facebook.php";

//New Facebook
$facebook = new Facebook(array(
'appId'     => $app_id,
'secret'    => $app_secret,
'cookie'    => TRUE, /* Optional */
'oath'      => TRUE  /* Optional */
));

//Login URL
$loginUrl = $facebook->getLoginUrl(array(
    'scope'     => 'email,user_birthday',
    'redirect_uri'  => $site_url,
    ));

//Get FacebookUserID
$fbuser = $facebook->getUser();

Important Note Also: I had some issues with Firefox $facebook->getUser() was 0 even my code was working on Chrome. But after cleaning Firefox cache, it worked nicely!

Hope it helps.

Nicholaos Renessis
  • 432
  • 1
  • 4
  • 8
0

It might be Worked using

$opts[CURLOPT_SSL_VERIFYPEER] = false;  after $opts = self::$CURL_OPTS; on file base_facebook.php. 
shankar.parshimoni
  • 1,289
  • 5
  • 22
  • 42
  • I don't understand your answer, could you be please more specific and elaborate? See the other answers, and look if you add something useful. – mliebelt Dec 26 '14 at 11:44