普通网友 2015-09-19 08:26
浏览 54

从Facebook令牌元数据响应中获取'user_id'值

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() . "
";
    echo "Error Code: " . $helper->getErrorCode() . "
";
    echo "Error Reason: " . $helper->getErrorReason() . "
";
    echo "Error Description: " . $helper->getErrorDescription() . "
";
  } 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"
  }
}
  • 写回答

3条回答 默认 最新

  • douhuo0884 2015-09-19 08:38
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败