需要有关 OAuthException 代码 2500 的帮助

发布于 2025-01-07 10:11:17 字数 2270 浏览 5 评论 0原文

我正在尝试使用 PHP 开发 Facebook 应用程序 (apps.facebook.com/some_app),其中我需要根据用户的音乐兴趣呈现一些信息。我发现它位于“user_likes > games”下。

我的问题如下:

  • 为了获得访问权限,我已将 oauth 对话框方法实现为 在我的索引页的 API 中建议。

$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
            . $app_id . "&redirect_uri=" 
            . urlencode($canvas_page)
            ."&scope=user_likes";
  • 授权成功后,我返回到带有“代码”的索引页面 作为参数。

http://MY_CANVAS_PAGE/?code=some base64 encoded letters
  • 首先我不知道我是否需要 access_token 来读取用户的音乐 兴趣,但我已经尝试了所有建议的方法。我无法动弹 从这一点开始,
  • 我有一个像这样的代码(在我的索引页面中),如果未设置代码参数,它将重定向以进行授权。

if(empty($code) && !isset($_REQUEST['error'])) {
  $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
  echo("<script> top.location.href='" . $auth_url . "'</script>");
}
  • 目前我只是想在这里获取用户的公共信息但是 没有成功。我已按照建议尝试了signed_request方法 但没有成功

$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
echo ("Welcome User: " . $data["user_id"]);

print_r($decoded_response);

stdClass Object ( [error] => stdClass Object ( [message] => An active 
access token must be used to query information about the current user. 
[type] => OAuthException [code] => 2500 ) ) 

要获取用户的公共信息,我也尝试了 PHP SDK 中建议的示例,


$facebook = new Facebook(array(
'appId'  => MY_APP_ID, //registered facebook APP ID
'secret' => MY_SECRET, //secret key for APP
));

$fb_user = $facebook->getUser();

if($fb_user){
    try {
      $user_profile = $facebook->api('/me');
      echo $user_profile['email'];
    }
    catch(FacebookApiException $e) {
      $fb_user = null;
    }
}

但没有成功。有人可以解释一下为什么我会收到此错误以及如何正确访问用户的音乐兴趣。可能我误解了API。

谢谢迪帕克

I am trying to develop an Facebook application (apps.facebook.com/some_app) using PHP where I need to present some information based on user's music interests. I found that its under "user_likes > games".

My problems are as follows:

  • To gain access, I have implemented the oauth dialog method as
    suggested in API in my index page.

$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
            . $app_id . "&redirect_uri=" 
            . urlencode($canvas_page)
            ."&scope=user_likes";
  • After successful authorization I come back to index page with "code"
    as parameters.

http://MY_CANVAS_PAGE/?code=some base64 encoded letters
  • Firstly I don't know if I need access_token just to read user's music
    interests but I have tried all the methods suggested. I couldn't move
    forward from this point
  • I have a code like this (in my index page), which redirects for authorization if code parameters is not set.

if(empty($code) && !isset($_REQUEST['error'])) {
  $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
  echo("<script> top.location.href='" . $auth_url . "'</script>");
}
  • Currently I am just trying to get user's public information here but
    with no success. I have tried the signed_request method as suggested
    but no success

$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
echo ("Welcome User: " . $data["user_id"]);

print_r($decoded_response);

stdClass Object ( [error] => stdClass Object ( [message] => An active 
access token must be used to query information about the current user. 
[type] => OAuthException [code] => 2500 ) ) 

To get user's public info, I have tried also the suggested example in PHP SDK


$facebook = new Facebook(array(
'appId'  => MY_APP_ID, //registered facebook APP ID
'secret' => MY_SECRET, //secret key for APP
));

$fb_user = $facebook->getUser();

if($fb_user){
    try {
      $user_profile = $facebook->api('/me');
      echo $user_profile['email'];
    }
    catch(FacebookApiException $e) {
      $fb_user = null;
    }
}

But no success. Can somebody explain me why I am getting this error and how to access the user's music interest properly. Probably I misunderstood the API.

Thanks

Deepak

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

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

发布评论

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

评论(4

对风讲故事 2025-01-14 10:11:17

错误 2500 表示您没有访问令牌或应用程序访问令牌,但正在尝试访问 /me/ - “me”是“当前用户或页面 ID”的占位符,因此如果没有访问令牌,则无效用户或页面的访问令牌。

要访问用户的点赞列表,您还需要 user_likes 权限 授权时

我发现应用程序无法将 code 交换为访问令牌的最可能原因是:

  1. 您使用的 redirect_uri 是文件夹或服务器根目录并且缺少尾部斜杠(即它是 http://www.example.com 而不是 http://www.example.com/
  2. )您在第一次调用身份验证对话框时使用的 与您在调用交换 access_token 代码时使用的 redirect_uri 并不完全相同

如果您的身份验证流程因某些原因而中断其他原因你不能从 SDK 示例或 身份验证 文档中找出答案,这可能需要一段时间才能完成与您交谈,因为您可能缺少一些理解他们的答案所需的背景知识

Error 2500 means you have no access token or an app access token but are trying to access /me/ - 'me' is a placeholder for 'current user or page ID' so won't be valid without an access token for a user or page.

To access the user's list of likes you'll also need the user_likes Permission when authorising them

The most likely causes i've seen for apps not being able to exchange the code for an access token are:

  1. The redirect_uri you used is to a folder or server root and is missing a trailing slash (i.e it's http://www.example.com instead of http://www.example.com/)
  2. The redirect_uri you used in the first call to the auth dialog wasn't precisely the same as the redirect_uri you used in the call to exchagne the code for an access_token

If your auth flow is broken for some other reason and you can't figure it out from the SDK examples or the Authentication documentation, this could take a while for someone to talk you through because you may be missing some background knowledge necessary to understand their answers

流年已逝 2025-01-14 10:11:17

对我来说,我尝试使用 php 获取身份验证令牌,发现 2500 错误是由于 file_get_contents 未返回任何内容的问题造成的。我可以在浏览器中访问该 URL,但不能使用 file_get_contents 来访问。我通过使用 CURL 来修复它,如下所述:https://stackoverflow.com/a/6325313

结果是我使用了 https://developers.facebook.com/ 中的代码docs/authentication/server-side/ 并将该行替换为:

$response = file_get_contents($token_url);

因此

$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

变成:

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

    /* Facebook say to use this, but file_get_contents isn't working!
    $response = file_get_contents($token_url,null, $val);
    */

    // Use this as an alternative
    $ch = curl_init($token_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);

    // From https://developers.facebook.com/docs/authentication/server-side/
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];

For me, I was trying to get an auth token with php and found out that the 2500 error was due to an issue with file_get_contents not returning anything. I could access the URL in my browser, but not by using file_get_contents. I fixed it by using CURL instead as described here: https://stackoverflow.com/a/6325313.

The result was that I used the code from https://developers.facebook.com/docs/authentication/server-side/ and replaced the line:

$response = file_get_contents($token_url);

with

$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

It therefore became:

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

    /* Facebook say to use this, but file_get_contents isn't working!
    $response = file_get_contents($token_url,null, $val);
    */

    // Use this as an alternative
    $ch = curl_init($token_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);

    // From https://developers.facebook.com/docs/authentication/server-side/
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];
浅笑依然 2025-01-14 10:11:17

尝试此代码

$code = $_REQUEST["code"];
  if(empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }

  $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);
    $access_token = $params['access_token'];

PS:用户电子邮件地址不是公共信息 &您需要 user_email 权限

try This code

$code = $_REQUEST["code"];
  if(empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }

  $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);
    $access_token = $params['access_token'];

PS:user email address is not a public info & you need user_email permission for it

故事与诗 2025-01-14 10:11:17

感谢 https://developers.facebook.com/blog/post/534 我已经解决了我的问题/

我刚刚复制了示例并粘贴到我的脚本中。它完全适合。它是 PHP 和 Javascript 的混合使用。

I have solved my problem thanks to https://developers.facebook.com/blog/post/534/

I just copied the sample and pasted into my script. It fully fits. It is kind of hybrid usage of PHP and Javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文