简体   繁体   English

可以找出用户是否通过javascript API登录facebook?

[英]Possible to find out whether a user is logged into facebook over javascript API?

This question is not a duplicate of this one . 这个问题不是一个重复的这一个

I don't want to know whether the user has authorized my application, but if the user is logged into facebook (completely independed from my application). 我不想知道用户是否已经授权我的应用程序,但是如果用户登录到facebook(完全独立于我的应用程序)。

The reason is that I want to pring user comments in my html code so that search engines can index them. 原因是我想在我的html代码中pring用户注释,以便搜索引擎可以索引它们。

When a user is logged into facebook I want to replace the html code with the facebook comments snippet. 当用户登录到Facebook时,我想用facebook评论片替换html代码。

If not an alternative old school comment form should be displayed. 如果没有,则应显示替代旧学校评论表。

I would pull the comments regularely from the graph api to have them in my database and comments that are done using the classic form should be posted over the api (not necessarily as the user, could be an admin account...) to have all the data synchronized. 我会从图表api中正确地提取评论,将它们放在我的数据库中,使用经典表单完成的评论应该通过api(不一定是用户,可能是管理员帐户......)发布,以便全部数据同步。

I looked at the Javascript SDK Docs, also found the function getloginstatus but the documentations are bad and not conclusive. 我查看了Javascript SDK Docs,也找到了函数getloginstatus,但是文档很糟糕而且没有定论。 I know that there are also often features available at facebook codes that are not documented or implemented in higher level apis. 我知道在Facebook代码中通常还有一些功能,这些功能没有记录或在更高级别的apis中实现。

My questions are: 我的问题是:

  • Can I somehow find out if a user is logged into facebook? 我可以以某种方式找出用户是否登录到Facebook?

  • Can I somehow have a callback or notification of posted comments, so I can trigger synchronization to my database or do I have to "crawl" the graph api on a regular basis? 我可以以某种方式回调或发布评论的通知,因此我可以触发同步到我的数据库或者我是否必须定期“抓取”图形api?

There is a non-hack, officially-supported way of doing this for Facebook (I think the last version of the docs was clearer on this point). 有一种非正式的,官方支持的方式为Facebook做这个(我认为最后一个版本的文档在这一点上更清晰)。 Using the Javascript SDK , you can do: 使用Javascript SDK ,您可以:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {

    FB.init({appId: 'YOUR APP ID', status: true, cookie: true,
             xfbml: true});

    FB.getLoginStatus(function(o) { 
       if (!o && o.status) return;
       if (o.status == 'connected') {
          // USER IS LOGGED IN AND HAS AUTHORIZED APP
       } else if (o.status == 'not_authorized') {
          // USER IS LOGGED IN TO FACEBOOK (BUT HASN'T AUTHORIZED YOUR APP YET)
       } else {
          // USER NOT CURRENTLY LOGGED IN TO FACEBOOK
       }
    });

  };

  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());

</script>

An aside: if XAuth had caught on, it would be possible to do this in a more universal and supported way for any site supporting that standard. 抛开:如果XAuth已经流行起来,那么对于支持该标准的任何站点,都可以以更通用和受支持的方式执行此操作。

This article 本文

https://grepular.com/Abusing_HTTP_Status_Codes_to_Expose_Private_Information https://grepular.com/Abusing_HTTP_Status_Codes_to_Expose_Private_Information

identifies security risks in Google and Facebook that will allow you to determine if a user is logged in. While no official API exists to check if a user is logged in without that user giving you express permission to access this information, the above article shows how you can 'guess' if a user is logged in or not. 识别Google和Facebook中的安全风险,以确定用户是否已登录。虽然没有官方API来检查用户是否已登录而该用户未明确允许您访问此信息,但上述文章说明了您可以“猜测”用户是否已登录。

Note: The article identifies a 'hack' and so is not guaranteed to work in the future, if or when Google & Facebook identify these security risks. 注意:如果Google和Facebook识别出这些安全风险,那么该文章会识别出“黑客”,因此无法保证将来可以使用。

I also ran into similar requirements and solved my problem with following code; 我也遇到了类似的要求,用以下代码解决了我的问题; Using the Javascript SDK, I used FB object. 使用Javascript SDK,我使用了FB对象。 FB is a facebook object, it has property called _userStatus, this can be used like following. FB是一个facebook对象,它有一个名为_userStatus的属性,可以像下面这样使用。

if(FB._userStatus == "connected")
{
// USER IS LOGGED IN AND HAS AUTHORIZED APP 
}
else if(FB._userStatus == "notConnected"){
   // USER IS LOGGED IN TO FACEBOOK (BUT HASN'T AUTHORIZED YOUR APP YET)  
}
else if(FB._userStatus == "unknown")
{
    // USER NOT CURRENTLY LOGGED IN TO FACEBOOK 
}

The above code is very useful. 上面的代码非常有用。 It can be used in any part of the page as long FB object is not null. 它可以在页面的任何部分使用,因为长FB对象不为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM