Grant Doole
Grant Doole

Reputation: 615

Not getting Facebook User ID

I'm trying to get the Facebook ID of the current user logged in and using my app so that it can be written to a database along with some other information.

I've tested it countless times for myself and it will get my ID and write it to the database, but if I ask someone else to use it... It won't get the ID of the user of my application...

Am I missing any extended permissions for this? This is all I have so far to get the user id.

Thanks.

<?php include ('includes/php/facebook.php'); ?>

<?php

$facebook = new Facebook(array(
  'appId' => 'XXXXXXXXXXXXXXXXX',
  'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
));

?>

<?php

    $user = $facebook->getUser();
    $userId = $user;
    //some code here...
?>

Upvotes: 0

Views: 796

Answers (2)

Stelian Matei
Stelian Matei

Reputation: 11623

If $userID is 0 or null, it means that the user is not authenticated.

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

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me/accounts');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl(array("domain" => 'www.myurl.com'));
} else {
  $loginUrl = $facebook->getLoginUrl(array("scope" => 'publish_stream,offline_access,manage_pages'));
}

You can download the SDK from http://developers.facebook.com/docs/reference/php/

It contains several examples.

Upvotes: 1

Sterling Hamilton
Sterling Hamilton

Reputation: 913

http://developers.facebook.com/docs/authentication/

By default, the user is asked to authorize the app to access basic information that is available publicly or by default on Facebook. If your app needs more than this basic information to function, you must request specific permissions from the user. This is accomplished by adding a scope parameter to the OAuth Dialog request followed by comma separated list of the required permissions. The following example shows how to ask for access to user's email address and their news feed:

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream

Upvotes: 0

Related Questions