FB 不返回我的应用程序的 access_token,未指定错误消息。

发布于 2025-01-03 19:45:15 字数 851 浏览 0 评论 0原文

这是我的第一个 FB 应用程序,网站集成,我无法获取 FB 的示例 php 代码来返回 access_token。

我已经检查过: -developer.facebook.com 上的应用程序设置屏幕看起来没问题(没有警告或错误) - 在页面上包含最新的 github 版本的 facebook.php - app_id 和 app_secret 正确

以下代码是从 FB 文档复制的,但 $response 不返回任何内容,并且 $user->name 为空。

有人可以帮我吗?是否有一种调试技术可以告诉我为什么无法成功获取 access_token?

$token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

 $response = @file_get_contents($token_url);

 $params = null;
 parse_str($response, $params);

 $graph_url = "https://graph.facebook.com/me?access_token=" 
   . $params['access_token'];

 $user = json_decode(file_get_contents($graph_url));
 echo("Hello " . $user->name)."<br>";

This is my first FB app, website integraton, and I can't get FB's sample php code to return an access_token.

I have checked:
- app setup screens on developer.facebook.com seem ok (no warnings or errors)
- include latest github version of facebook.php on page
- app_id and app_secret are correct

The code below is copied from the FB docs, but $response doesn't return anything and $user->name is empty.

Can anyone give me a hand here? Is there a debug technique that could tell me why it's failing to succeed in getting a access_token?

$token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

 $response = @file_get_contents($token_url);

 $params = null;
 parse_str($response, $params);

 $graph_url = "https://graph.facebook.com/me?access_token=" 
   . $params['access_token'];

 $user = json_decode(file_get_contents($graph_url));
 echo("Hello " . $user->name)."<br>";

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

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

发布评论

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

评论(1

定格我的天空 2025-01-10 19:45:15

尝试以下代码代替上面的代码

<?php 

require_once 'library/facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => 'app id',
  'secret' => 'secret',
  'cookie' => true,
)); 

     $app_id = '149865361795547';

     $canvas_page = "canvas page link ";

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

     $signed_request = $_REQUEST["signed_request"];

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

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

     if (empty($data["user_id"])) {
            echo("<script> top.location.href='" . $auth_url . "'</script>");
     } else {

    //get the user access token
            $atoken = $facebook->getAccessToken();
            echo "</br>" . 'User Access_Token:' . $atoken;

     //get the user id 
            $UserId = $data["user_id"];
            echo 'UserId;' . $UserId;
}
?>

Instead of above code try the below

<?php 

require_once 'library/facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => 'app id',
  'secret' => 'secret',
  'cookie' => true,
)); 

     $app_id = '149865361795547';

     $canvas_page = "canvas page link ";

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

     $signed_request = $_REQUEST["signed_request"];

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

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

     if (empty($data["user_id"])) {
            echo("<script> top.location.href='" . $auth_url . "'</script>");
     } else {

    //get the user access token
            $atoken = $facebook->getAccessToken();
            echo "</br>" . 'User Access_Token:' . $atoken;

     //get the user id 
            $UserId = $data["user_id"];
            echo 'UserId;' . $UserId;
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文