检查Facebook会话加载
check facebook session onload
下面我正在尝试检查页面加载时Facebook的会话。并希望根据登录状态在页面上显示不同的内容。怎么了?
我想前两个检查用户连接状态。然后只在页面上显示适当的内容。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
</head>
<body>
<script>
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '1781292862', // App ID from the app dashboard
// channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms
status : true, // Check Facebook Login status
xfbml : true // Look for social plugins on the page
});
// Additional initialization code such as adding Event Listeners goes here
};
window.onload(){
FB.init({
appId : '178812862', // Your FB app ID from www.facebook.com/developers/
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert("yes");
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
} else {
// the user isn't logged in to Facebook.
}
});
</script>
</body>
</html>
首先,您需要使用 response.status
而不是 response.session
。由于状态将是字符串("已连接"、"not_authorized"或"未知"),因此您需要相应地设置 if
语句。以下示例来自 https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
让我知道这是否有意义,或者如果您有任何问题:)
更新
您的脚本仍然缺少将 facebook sdk 加载到文档中的小闭包(这就是您收到未定义 FB 的错误的原因)。尝试将整个脚本块替换为以下内容:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 1343434343, // Your FB app ID from www.facebook.com/developers/
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert("yes");
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
} else {
// the user isn't logged in to Facebook.
}
});
};
// Load the SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
(^最后一部分是加载Facebook SDK的部分。尝试一下,让我知道它是如何进行的:)
试试这个
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
id= response.id;
if(id==undefined)
{
alert('I am logged out');
}
else
{
alert('I am logged in');
}
})
}
});
相关文章:
- 使用Jquery在页面加载上测试php会话变量
- 如何在Ruby on Rails会话过期时重新加载或刷新页面
- 每个用户/浏览器会话只加载一次Javascript
- 如何生成一个唯一的链接,该链接将加载一个会话,其中包含客户端可用的某些文档
- 流星铁路由器 - 如何等待(使用加载模板)一个会话.get
- 会话存储未保存或未加载
- 有时会加载旧的 PHP 会话变量
- 在 chrome 中使用会话和本地存储时未加载 Angular JS 页面
- 弹出式:每个会话和加载仅一次
- 重新加载页面后会话变量为空
- 检查Facebook会话加载
- 将会话变量发送到另一个从未加载的 PHP 页面
- jquery / javascript 根据会话加载不同的内容
- 打开新窗口并从父窗口加载CSS和js,还可以共享会话
- 加载php文件,以获得我的会话使用javascript
- 在会话中传递所选国家(下拉)的值,然后重新加载当前页面(Rails 4)
- 限制名称和日期js cookie每次会话只加载一次
- 使用Javascript为整个会话加载网站字体
- 会话值不是在JSP页面中更新,而是在页面重新加载后更新
- 在Backbone JS中保持跨页面重新加载会话