0

enter image description here

// Function for facebook login
    function facebook_login() {
      $output = array();
      // Create facebook object for user login.
      $config = array();
      $config['appId' ] = 'XXXXXXX';
      $config['secret'] = 'XXXXXXX';
      $config['cookie'] = true;

      session_start();
      $code = $_GET['code'];

      $facebook_user = null;

    // Create the facebook user object
      $facebook = new Facebook(array(
        'appId' => $config['appId'],
        'secret' => $config['secret'],
        'cookie' => $config['cookie'],
      ));


      // Get User ID
      $facebook_user = $facebook->getUser();

    // If facebook user id not equal to 0
      if ($facebook_user) {
        try {
          // Proceed knowing you have a logged in user who's authenticated.
          $user_profile = $facebook->api('/me');
          $output['facebookurl'] = 'user/register';

          return $output; // Returns a result.
        }
        catch (FacebookApiException $e) {
          drupal_set_message($e, 'error');
          $facebook_user = null;
        }
      }
      else {
        $facebook_params = array(
          'scope' => 'email',
          'redirect_uri' => 'http://localhost/mywebsite',
          'display' => 'popup',
          'code' => $code,
        );

        $output['facebookurl'] = $facebook->getLoginUrl($facebook_params); // Returns the facebook login url as result.
      }
    }

Here is my php code for facebook login. Certain event will call the facebook_login() function and will do some processing based on the $output result.

The problem is $facebook_user is always 0, i.e. I always get the getLoginUrl as output. I have tried all the answers in on stackoverflow related to this issue, but none helped me.

I am testing this app on my localhost and have set the Site URL to http://localhost/mywebsite in facebook app settings.

Please help!

subhojit777
  • 586
  • 3
  • 11
  • 28

1 Answers1

1

When you are running this code, are you logged into facebook and following the login URL? You The User ID will only be returned if the App has been added by the user and the cookie has been set on your site.

Running this on Localhost will work fine as long as your app settings on FB are configured to point to localhost too.

Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
  • I have uploaded a screenshot of my app settings in facebook. I have not specified the app domain, is it necessary to do so? – subhojit777 Aug 07 '12 at 12:23
  • Thanks. Your settings look correct. What is returned by `getLoginUrl();`? – Niraj Shah Aug 07 '12 at 12:49
  • https://www.facebook.com/dialog/oauth?client_id=XXXXX&redirect_uri=http%3A%2F%2Flocalhost%2Fmywebsite&state=87f06f869ba01a25a9a525b607af376c&scope=email – subhojit777 Aug 07 '12 at 13:00
  • If the user authorised to use the app? I.e. logged in? – Niraj Shah Aug 07 '12 at 13:03
  • I am logged out of facebook. After I click the login url, the page gets refreshed. In other tab I open facebook.com and I see that I am logged in. But the getUser is returning 0. Some more information, I am working on Drupal. Thanks – subhojit777 Aug 07 '12 at 13:07
  • Must be a problem with the way you are using Facebook with Drupal. – Niraj Shah Aug 07 '12 at 14:34