How can I detect that a user signed into Facebook?

一曲冷凌霜 提交于 2019-12-08 03:10:10

问题


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.

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.

I'd prefer JavaScript client code.


回答1:


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.


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>



回答2:


You can subscribe to the auth.login event. http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/




回答3:


There are many Facebook images that you can see only if you're logged into Facebook. You can make the user attempt to load one of these images, and then detect if it loaded or not. It's a total trick.



来源:https://stackoverflow.com/questions/5609976/how-can-i-detect-that-a-user-signed-into-facebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!