Facebook PHP SDK:api('/me') 奇怪的行为

发布于 2024-12-27 10:51:13 字数 1215 浏览 1 评论 0原文

因为我真的不明白 $user = $facebook->getUser();函数(即使用户已登录,它也会返回 NULL...)我正在尝试使用此代码。问题是,它给出了“必须使用活动访问令牌来查询有关当前用户的信息”。 OAuthException 并且如果我将登录重定向代码也放在 Catch 部分中,它会进入无限的重定向循环...有人可以帮助我吗?

谢谢你! :)

try {
    $user_profile = $facebook->api('/me');
    $scope = 'publish_stream,user_photos';

    $scope_params = explode(',',$scope);

    $permissions = $facebook->api("/me/permissions");

if( array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('user_photos', $permissions['data'][0])) {
  // facebook logic
 }else {
    ?>
    <!doctype html>
        <html xmlns:fb="http://www.facebook.com/2008/fbml">
        <head>
        <title>Redirect</title>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 

        <script language=javascript>window.open('<?php echo $loginUrl ?>', '_parent', '')</script>
        </head>
        <body>
        Redirecting...
        </body>
    </html>

        <?php
    exit;   
      }

}catch (FacebookApiException $e) {
     echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
}

Since i really don't understand the $user = $facebook->getUser(); function (it returns NULL even if the user is logged in...) I'm trying with this code. The problem is, that it gives the "An active access token must be used to query information about the current user." OAuthException and if i put the login redirect code also in the Catch section it gets into a endless redirecting loop... Can someone help me?

Thank you! :)

try {
    $user_profile = $facebook->api('/me');
    $scope = 'publish_stream,user_photos';

    $scope_params = explode(',',$scope);

    $permissions = $facebook->api("/me/permissions");

if( array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('user_photos', $permissions['data'][0])) {
  // facebook logic
 }else {
    ?>
    <!doctype html>
        <html xmlns:fb="http://www.facebook.com/2008/fbml">
        <head>
        <title>Redirect</title>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 

        <script language=javascript>window.open('<?php echo $loginUrl ?>', '_parent', '')</script>
        </head>
        <body>
        Redirecting...
        </body>
    </html>

        <?php
    exit;   
      }

}catch (FacebookApiException $e) {
     echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
}

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

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

发布评论

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

评论(3

感情洁癖 2025-01-03 10:51:13

您需要将活动访问令牌传递给 Graph API。每个访问令牌都有过期时间,过期后您需要重新进行身份验证。您应该检查 facebook 的 PHP SDK 中是否存在访问令牌 ($facebook->getAccessToken();)。如果不存在 - 尝试重新验证自己的身份。如果 id 没有帮助,请尝试使用另一个新的 Facebook 应用程序。整个身份验证如下所述: http://developers.facebook.com/docs/authentication/

上面的链接很好地描述了手动获取访问权限,而不使用 PHP SDK 令牌。

You need to pass active access token to the Graph API. Every access token has expiration time, and after it passes, you need to re-authenticate yourself. You should check for presence of access token in facebook's PHP SDK ($facebook->getAccessToken();). If it's not there - try to re-authenticate yourself. If id does not help, try using another, fresh facebook application. Whole authentication is described here: http://developers.facebook.com/docs/authentication/

Acquiring access by hand, without using PHP SDK token is nicely described in the link above.

缪败 2025-01-03 10:51:13

解决方案是将所有代码放入一个index.php文件中

solution was to put all the code into one index.php file

毅然前行 2025-01-03 10:51:13

循环是这样发生的:

  1. 调用 API
  2. API 说令牌已过期/无效
  3. GetLoginUrl() -> redirect
  4. 一旦重定向到oauth页面,它认为权限和token仍然有效;因此它会重定向回您的应用程序
  5. 发生循环(返回步骤1)

似乎在令牌过期或用户取消对应用程序授权的时间与oauth脚本实际获取更改的时间之间存在延迟。

也许是虫子?

The loop happens like this:

  1. Call API
  2. API says token expired / invalid
  3. GetLoginUrl() -> redirect
  4. Once redirected to the oauth page, it thinks that permissions and token still valid; so it redirects back to your app
  5. Loop occurs (back to step 1)

Seems there is a delay between the time a token expires or a user deauthorises an app and when the oauth script actually picking up the change.

Bug Maybe?

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