如何检测到用户登录了Facebook? [英] How can I detect that a user signed into Facebook?

查看:127
本文介绍了如何检测到用户登录了Facebook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况.当用户打开浏览器并访问facebook.com进行登录时,我希望能够检测到该信息并启动我的Facebook应用程序登录过程.

Here is my scenario. When a user opens a browser and goes to facebook.com to sign himself in, I want to be able to detect that and init my Facebook application sign-in process.

有可能吗?我之所以问是因为我注意到登录Facebook本身并不能自动登录我的应用程序,这不好.

Is that possible? I'm asking because I noticed that signing into Facebook itself doesn't get my app signed in automatically, which is not good.

我更喜欢JavaScript客户端代码.

I'd prefer JavaScript client code.

推荐答案

更新:在这里,您有一个全职工作示例.您唯一需要做的就是将APP_ID替换为在以下位置登录您的应用程序时获得的ID: http://www.facebook.com/developers/以及您可以用于localhost测试的端口.即使在另一个浏览器窗口中,也可以检测到用户何时登录.显然,这是非常基础的,但是证明了使用图API的工具是可行的.

UPDATE: Here you have a FULLY WORKING example. The only thing you will need to do is to replace the APP_ID for whatever ID you get when sign your app at: http://www.facebook.com/developers/ and the port you may use for the localhost test. This detects when the user logs in even in another browser window. It is obviously extremely basic but proves the point that is feasible with the tools from the graph API.

您可以添加以下代码来检测用户何时登录.用户单击<fb:login-button>还是直接登录facebook.com,这与oauth登录相同:

You can add this code to detect when the user logs in. It is the same oauth log in whether the user clicks on a <fb:login-button> or logs directly onto facebook.com:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Hello App Engine</title>
        <SCRIPT type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></SCRIPT>
  </head>

  <body>
    <h1>Hello!</h1>

<div id="fb-root"></div>
<script type="text/javascript">
FB.init({
    appId  : APP_ID,
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
});

/*
 * Authentication functions
 */
FB.Event.subscribe('auth.login', function (response) {
    welcomeMsg(response);
});

FB.Event.subscribe('auth.logout', function (response) {
    alert ("Good bye!");
});

if (FB.getSession() != null) {
    FB.api('/me', function(response){
        console.log(response.name);
        welcomeMsg(response);

    })
} else {
    window.setTimeout(function(){window.location.href="http://localhost:8888"},5000);
}


function welcomeMsg(userData) {
    console.log ("Welcome " + userData.name);
}
    </script>
  </body>
</html>

这篇关于如何检测到用户登录了Facebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆