我的 Facebook 图表 URL 有问题

发布于 2024-12-26 04:06:11 字数 1919 浏览 1 评论 0原文

当尝试从 facebook graph API 访问信息时,我显然做错了一些事情,这是我页面的代码:

它获取除用户电子邮件地址之外我需要的所有内容,我知道您需要扩展权限,我已经请求了这些权限,如图所示在我下面的代码中。

require 'src/facebook.php';
$app_id = "211665122244023";


     $canvas_page = "http://apps.facebook.com/midcitymafia/";

     $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
            . $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=email,publish_actions";

     $signed_request = $_REQUEST["signed_request"];

     list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

     $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

$user_id = $data["user_id"];
     if (empty($data["user_id"])) {
            echo("<script> top.location.href='" . $auth_url . "'</script>");
     } else {
                         $graph1 = file_get_contents ("https://graph.facebook.com/".$user_id . "/?accesstoken=" . $data["oauth_token"]);
         $graph=json_decode($graph1,true);
     }



$userid =  $user_id;
$username = $graph['name'];
$usergender = $graph['gender'];
$useremail = $graph['email'];

?>

<br>

<?php echo 'ID: ' . $userid; ?>
<br>
<?php echo 'Name: ' . $username; ?>
<br>
<?php echo 'Gender: ' . $usergender; ?>
<br>
<?php echo 'Email: ' . $useremail; ?>

我转储 $graph 变量数组以查看它保存的信息,结果是:

array(7) { ["id"]=> string(10) "1469088864" ["name"]=> string(10) "Jack Brown" ["first_name"]=> string(4) "Jack" ["last_name"]=> string(5) "Brown" ["username"]=> string(11) "thebestjack" ["gender"]=> string(4) "male" ["locale"]=> string(5) "en_GB" }

它不包含电子邮件地址,因此访问令牌/使用访问令牌访问图形显然存在问题。

当我在 Facebook 帐户设置中访问此应用程序下的授权应用程序时,它显示“此应用程序需要您的电子邮件地址”[电子邮件受保护]"

I'm obviously doing something wrong when trying to access information from the facebook graph API, here is the code of my page:

It fetches everything I need other than the users email address, I understand you need extended permissions which I have requested as seen in my code below.

require 'src/facebook.php';
$app_id = "211665122244023";


     $canvas_page = "http://apps.facebook.com/midcitymafia/";

     $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
            . $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=email,publish_actions";

     $signed_request = $_REQUEST["signed_request"];

     list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

     $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

$user_id = $data["user_id"];
     if (empty($data["user_id"])) {
            echo("<script> top.location.href='" . $auth_url . "'</script>");
     } else {
                         $graph1 = file_get_contents ("https://graph.facebook.com/".$user_id . "/?accesstoken=" . $data["oauth_token"]);
         $graph=json_decode($graph1,true);
     }



$userid =  $user_id;
$username = $graph['name'];
$usergender = $graph['gender'];
$useremail = $graph['email'];

?>

<br>

<?php echo 'ID: ' . $userid; ?>
<br>
<?php echo 'Name: ' . $username; ?>
<br>
<?php echo 'Gender: ' . $usergender; ?>
<br>
<?php echo 'Email: ' . $useremail; ?>

I dumped the $graph variable array to see what information it held, this was the result:

array(7) { ["id"]=> string(10) "1469088864" ["name"]=> string(10) "Jack Brown" ["first_name"]=> string(4) "Jack" ["last_name"]=> string(5) "Brown" ["username"]=> string(11) "thebestjack" ["gender"]=> string(4) "male" ["locale"]=> string(5) "en_GB" }

It doesn't contain the email address so there is obviously a problem with the access token / using the access token to access the graph.

When I go to my authorised apps on my Facebook account settings under this app it says "this apps needs your email address "[email protected]"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2025-01-02 04:06:11

对于任何请求,您都有一个 $_REQUEST["signed_request"],无论用户是否授权您的应用。即使根本没有登录 Facebook 的用户也可以使用它。

使用 php-sdk 检索用户的个人资料 http://developers.facebook.com/文档/参考/php/facebook-api/

<?php
require_once('php-sdk/facebook.php');

$config = array(
  'appId' => '211665122244023',
  'secret' => 'YOUR_APP_SECRET',
);

$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$login_url = $facebook->getLoginUrl(array(
    scope => 'email,publish_actions',
    redirect_uri => $canvas_page,));

if($user_id) {
  try {
    $user_profile = $facebook->api('/' . $user_id,'GET');
    $userid =  $user_id;
    $username = $user_profile['name'];
    $usergender = $user_profile['gender'];
    $useremail = $user_profile['email'];

  } catch(FacebookApiException $e) {
    echo("<script> top.location.href='" . $login_url . "'</script>");
    error_log($e->getType());
    error_log($e->getMessage());
  }   
} else {
    echo("<script> top.location.href='" . $login_url . "'</script>");
}
?>
<?php if (isset($useremail)) : ?>
<br>
<?php echo 'ID: ' . $userid; ?>
<br>
<?php echo 'Name: ' . $username; ?>
<br>
<?php echo 'Gender: ' . $usergender; ?>
<br>
<?php echo 'Email: ' . $useremail; ?>
<?php endif; ?>

you have a $_REQUEST["signed_request"] for any request, doesn't matter if user authorized your app or not. you have it even for users who are not logged in to Facebook at all.

use php-sdk to retrieve user's profile http://developers.facebook.com/docs/reference/php/facebook-api/

<?php
require_once('php-sdk/facebook.php');

$config = array(
  'appId' => '211665122244023',
  'secret' => 'YOUR_APP_SECRET',
);

$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$login_url = $facebook->getLoginUrl(array(
    scope => 'email,publish_actions',
    redirect_uri => $canvas_page,));

if($user_id) {
  try {
    $user_profile = $facebook->api('/' . $user_id,'GET');
    $userid =  $user_id;
    $username = $user_profile['name'];
    $usergender = $user_profile['gender'];
    $useremail = $user_profile['email'];

  } catch(FacebookApiException $e) {
    echo("<script> top.location.href='" . $login_url . "'</script>");
    error_log($e->getType());
    error_log($e->getMessage());
  }   
} else {
    echo("<script> top.location.href='" . $login_url . "'</script>");
}
?>
<?php if (isset($useremail)) : ?>
<br>
<?php echo 'ID: ' . $userid; ?>
<br>
<?php echo 'Name: ' . $username; ?>
<br>
<?php echo 'Gender: ' . $usergender; ?>
<br>
<?php echo 'Email: ' . $useremail; ?>
<?php endif; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文