【问题标题】:Facebook SDK PHP Display a users email addressFacebook SDK PHP 显示用户电子邮件地址
【发布时间】:2017-09-08 02:46:30
【问题描述】:

所以我使用 Facebook SDK 进行 Facebook 登录,并按照他们的文档显示,尽管我似乎无法弄清楚如何回显用户的电子邮件地址?

我可以看到它确实收到了电子邮件,尽管电子邮件地址没有显示在 METADATA 中:

object(Facebook\Authentication\AccessTokenMetadata)#17 (1) { ["metadata":protected]=> array(7) { ["app_id"]=> string(15) "1234565678" ["application"]=> string(4) "PLFT" ["expires_at"]=> object(DateTime)#21 (3) { ["date"]=> string(26) "2017-06-11 10:47:32.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["is_valid"]=> bool(true) ["issued_at"]=> object(DateTime)#22 (3) { ["date"]=> string(26) "2017-04-12 10:47:32.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["scopes"]=> array(2) { [0]=> string(5) "email" [1]=> string(14) "public_profile" } ["user_id"]=> string(16) "36346346346346" } } 

这是我正在使用的脚本:

<?
session_start();
include 'system/db.php';
require_once 'fb_connect/autoload.php';

$fb = new Facebook\Facebook([
  'app_id' => '346364326346346',
  'app_secret' => 'reyeryeryeryeerwyer346436',
  'default_graph_version' => 'v2.5',
]);

$helper = $fb->getRedirectLoginHelper();

try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo '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');
    echo 'Bad request';
  }
  exit;
}

// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());

// 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);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('43141346436316'); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();

if (! $accessToken->isLongLived()) {
  // Exchanges a short-lived access token for a long-lived one
  try {
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
    echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
    exit;
  }

  echo '<h3>Long-lived</h3>';
  var_dump($accessToken->getValue());
}

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


// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');

?>

知道如何在网站上显示用户数据吗?

【问题讨论】:

    标签: php facebook api email sdk


    【解决方案1】:

    获得访问令牌后,您可以将其传递给图形 API,以通过传递令牌获取用户详细信息。

          <?php
    
          $fb = new \Facebook\Facebook([
              'app_id' => '',
              'app_secret' => '',
              'default_graph_version' => 'v2.8'
          ]);
    
          try {
              // Get the \Facebook\GraphNodes\GraphUser object for the current user.
              // If you provided a 'default_access_token', the '{access-token}' is optional.
              $response = $fb->get('/me?fields=name,email,location,gender,birthday,hometown', $access_token);
          } catch(\Facebook\Exceptions\FacebookResponseException $e) {
              echo $e->getMessage();
          } catch(\Facebook\Exceptions\FacebookSDKException $e) {
              // When validation fails or other local issues
              echo $e->getMessage();
          }
    
          // Get user details from facebook.
          $me = $response->getGraphUser();
    
         print_r($me);
    

    ?>

    【讨论】:

    • 感谢您的快速回复,尽管我似乎只获得了用户的姓名、性别和 ID,即使我已请求访问电子邮件。 Facebook\GraphNodes\GraphUser 对象([items:protected] => 数组([name] => Aidan Thompson [gender] => male [id] => 17346346346346))
    • 您可以检查您的应用是否有权访问电子邮件。要检查此访问您的 FB 应用程序并单击“应用程序审查”并查看“已批准项目部分”
    • 它似乎有权限,默认情况下是批准的,所以我在这里有点困惑。
    • 通过点击“公开...公开?”公开您的应用按钮。
    • 如果仍然没有得到,那么您需要“提交项目以供批准”。
    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多