boo*_*ots 2 javascript facebook fbjs
这是我的情景.当用户打开浏览器并前往facebook.com进行自我登录时,我希望能够检测到并启动我的Facebook应用程序登录过程.
那可能吗?我问,因为我注意到登录Facebook本身并没有让我的应用程序自动登录,这是不好的.
我更喜欢JavaScript客户端代码.
更新:这里有一个完整的工作示例.您需要做的唯一事情是将APP_ID替换为您在应用程序签名时获得的任何ID:http://www.facebook.com/developers/以及您可能用于localhost测试的端口.这将检测用户何时登录其他浏览器窗口.这显然是非常基础的,但证明了使用图API中的工具可行的观点.
您可以添加此代码以检测用户何时登录.无论用户是单击<fb:login-button>还是直接登录到facebook.com ,都是相同的oauth登录:
<!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>
Run Code Online (Sandbox Code Playgroud)