Red Virus
Red Virus

Reputation: 1707

Get the 'user_id' value from Facebook Token Meta Data Response

I' am using the following Facebook SDK to Login with Facebook for my Website. I found the source code on Facebook Developers.

$fb = new Facebook\Facebook([
    'app_id' => $fbapp_id,
    'app_secret' => $fbapp_secret,
    'default_graph_version' => 'v2.4',
]);

$helper = $fb->getRedirectLoginHelper();

try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  $msg = 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  $msg = 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

if ( !isset($accessToken) ) {
  if ($helper->getError()) {
    header('HTTP/1.0 401 Unauthorized');
    echo "Error: " . $helper->getError() . "\n";
    echo "Error Code: " . $helper->getErrorCode() . "\n";
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
  } else {
    header('HTTP/1.0 400 Bad Request');
    $msg = 'Bad request';
  }
  exit;
}

// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
var_dump($tokenMetadata);

$_SESSION['fb_access_token'] = (string) $accessToken;

By using the above code, I get the following response. How can I get the get the 'user_id' value from this array.

string(208) "CAAQJ73gQW1kBAEeG6aOH5aPZBmULTxJPuJ8qOliC1Xn5ljjBYVHHuXuiKAn04Dz2D6hdcZBiHqhtLhe8oR1b3M78KxxUsKOj3QzQsPEnPTqxw54MK026ljHxz6EbACMdgNYgQ0jO6x6x5YGdkdIari6Nhya8ea68gTpHArl8MxexXnkZCpBXOQxZAXQ4y80YNVZCsR9X89QZDZD"
object(Facebook\Authentication\AccessTokenMetadata)#13 (1) {
  ["metadata":protected]=>
  array(7) {
    ["app_id"]=>
    string(16) "1136824023014233"
    ["application"]=>
    string(8) "example.com"
    ["expires_at"]=>
    object(DateTime)#17 (3) {
      ["date"]=>
      string(26) "2015-11-18 06:36:01.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["is_valid"]=>
    bool(true)
    ["issued_at"]=>
    object(DateTime)#18 (3) {
      ["date"]=>
      string(26) "2015-09-19 06:36:01.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["scopes"]=>
    array(2) {
      [0]=>
      string(5) "email"
      [1]=>
      string(14) "public_profile"
    }
    ["user_id"]=>
    string(17) "1020772222323227"
  }
}

Upvotes: 1

Views: 3032

Answers (3)

Hugo Pakula
Hugo Pakula

Reputation: 385

I use the following code to convert the token metadata into an array:

$responseArray = (array) $tokenMetadataObject;
$token_metadata = $responseArray[array_keys($responseArray)[0]];

This gives you an array (in $token_metadata) with all of the fields now accessible (this method is a bit of a workaround, but it works). The reason you are unable to access the fields directly is because they are all private within the object, and only accessible from within the class.

If you try and convert it to an array and then directly access the fields, you still will not be able to get the value using the usual method of $theArray['theIndex'] because converting a private field to an array pads the index with NULL bytes so that you will not be able to access it.

Upvotes: 2

Scuzzy
Scuzzy

Reputation: 12332

Assuming this is the right class https://github.com/facebook/facebook-php-sdk-v4/blob/5.0-dev/src/Facebook/Authentication/AccessTokenMetadata.php

$tokenMetadata->getField('user_id')

To fetch all of the metadata use

$tokenMetadata->getMetadata()

This returns an associative array of all fields

Upvotes: 4

Red Virus
Red Virus

Reputation: 1707

I have managed to get the User Id using the following

$getUserId = $tokenMetadata->getUserId();

Upvotes: 6

Related Questions