Facebook access_token 请求 Facebook 好友时网站出现奇怪问题

发布于 2025-01-06 03:29:48 字数 1763 浏览 0 评论 0原文

我有一个用 symfony 2 编写的应用程序(并使用 fosfacebookbundle 和来自 fosub 的自定义 userprovider)。我在该应用程序中使用来自 facebook 的 js sdk 和 php sdk。 登录我的应用程序并可以进行冲浪。我有一个页面,其中 ai 显示用户的 Facebook 好友。在我的后端,我使用 /me/friends 来满足该请求。 假设我在该页面上停留了大约一个小时,该页面显示朋友没有任何活动。如果我在那个小时之后刷新页面,我会收到消息:

An exception has been thrown during the rendering of a template ("An active access token must be used to query information about the current user.") in 

这让我感到困惑。我仍然可以访问我网站上没有直接“Facebook 请求”的其他页面。如果我在其他页面上 var_dump facebook access_token,它们看起来会有所不同,例如,如果我在同一浏览器中使用 facebook 登录并通过 facebook 登录到我的应用程序,我会在显示的页面上获得以下 access_token用户朋友: AAACbRz2shZAIBAFX66jLuQktdlzzIx52lnSvgZB5hiNNxvy0lPbKMcUsC1BHLTnaAllXI9WN7dRHfa5TOj4HmZBjdwgjP5In46dMYSGZBfWrj

facebook 在同一浏览器的另一个选项卡中打开:如果我现在从 facebook 注销并刷新我网站上的页面,其中显示了用户的朋友,我收到上述异常消息。如果我浏览到另一个页面,并 var_dump the access_token,我会得到这个: string(48) "170732376699858|6c2b4d7ca20f8a607418845634356"

因此,仅显示用户 Facebook 好友的页面无法正常工作。

在 js 网站上,我正在执行以下操作:

    function onFbInit() {  

  if (typeof(FB) != 'undefined' && FB != null ) {

        FB.Event.subscribe('auth.authResponseChange', function(response) {
            $.each(reponse, function(e) {
                alert(e);
                });
            });

      FB.Event.subscribe('auth.statusChange', function(response) {
          if (response.session || response.authResponse) {
              setTimeout(goLogIn, 500);
          } else {
              window.location = "{{ path('_security_logout_fb') }}";
          }
      });
  } else  {
      setTimeout(goLogIn, 500);
      }
     }

知道我做错了什么吗? 有没有办法通过 php sdk 检查 access_token 是否有效?我通读了文档,但不知何故我没有找到方法。

我没有使用离线 acces_token 因为它会过时

谢谢, 拉莫

i have an application written with symfony 2 (and using an fosfacebookbundle with custom userprovider from fosub). i use the js sdk and the php sdk from facebook in that applicatoin.
log in into my app and surfing around is possible. I have one page where a i show a users facebook friends. in my backend i use /me/friends for that request.
lets say i stay about an hour on that page that shows the friends without any activity. if i refresh the page after that hour, i get the message:

An exception has been thrown during the rendering of a template ("An active access token must be used to query information about the current user.") in 

that confuses me somehow. i still can visit the other pages of my website where no direct "facebook requests" are done. If i var_dump the facebook access_token on my other pages, they're looking different, e.g. if i'm logged in with facebook in the same browser and log in into my aplication via facebook, i get the following access_token on the page where i display the users friend:
AAACbRz2shZAIBAFX66jLuQktdlzzIx52lnSvgZB5hiNNxvy0lPbKMcUsC1BHLTnaAllXI9WN7dRHfa5TOj4HmZBjdwgjP5In46dMYSGZBfWrj

facebook is opened in another tab in the same browser: if i log out now from facebook and refresh the page on my website where the users friends are shown, i get the above exception message. if i browse to another page, and var_dump the access_token, i get this one:
string(48) "170732376699858|6c2b4d7ca20f8a607418845634356"

so, just the page where the users facebook friends are shown is not working properly.

on js site i'm doing the following:

    function onFbInit() {  

  if (typeof(FB) != 'undefined' && FB != null ) {

        FB.Event.subscribe('auth.authResponseChange', function(response) {
            $.each(reponse, function(e) {
                alert(e);
                });
            });

      FB.Event.subscribe('auth.statusChange', function(response) {
          if (response.session || response.authResponse) {
              setTimeout(goLogIn, 500);
          } else {
              window.location = "{{ path('_security_logout_fb') }}";
          }
      });
  } else  {
      setTimeout(goLogIn, 500);
      }
     }

any ideas what i'm doing wrong?
Is there a way via php sdk to check if the access_token is valid? I read through the docs, but somehow i didn't find a way.

I'm not using the offline acces_token because it'll be obsolete

Thanks,
Ramo

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

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

发布评论

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

评论(1

你的呼吸 2025-01-13 03:29:48

这实际上听起来完全正确。由于您不使用 Offline_access 权限(而且您不这样做是正确的),Facebook 向每个用户授予您的令牌会在一段时间后过期,正如您所描述的。

在服务器端和客户端,您有多种选择来处理此问题。
但由于您是在服务器端发出 api 请求,因此您需要首先查明您要使用的访问令牌是否有效,有一篇很好的官方博客文章解释了如何操作: http://developers.facebook.com/blog/post/500/

至于为什么你只在该页面上收到错误页面而不是您网站中的其他页面,它可能是因为您在发出 facebook api 请求时不会尝试捕获异常,然后返回一个错误,但由于这是您发出该请求的唯一位置,因此这是唯一损坏的页面。

This actually sounds exactly right. Since you don't use the offline_access permission (and you are right not to) the tokens that you are granted with by facebook per user expire after some time, as you described.

You have a few options to handle this issue, both at the server side and in the client side.
But since you are making the api request at the server side you need to first find out if the access token you are about to use is valid, there's a good official blog post that explains how: http://developers.facebook.com/blog/post/500/

As for why you get an error page only on that page and not other pages in your site, it's probably because you don't try to catch an exception when making the facebook api request, which then returns an error, but since it's the only place you make that request, that's the only broken page.

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