Facebook JavaScript SDK Login
Facebook JavaScript SDK Login
这是我的代码。它可以完美地运行在Facebook的JavaScript SDK测试控制台,但不是在这里:http://www.booktrolley.in/beta/fblogin.php
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head></head>
<body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : '270423476307006',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
channelUrl : 'http://pedurology.org/booktrolley/beta/channel.html', // channel.html file
oauth : true // enable OAuth 2.0
});
</script>
<h1>Login, Logout, Name and Profile Picture</h1>
<div id="account-info"></div>
<script>
/**
* This assumes the user is logged in and renders their profile picture,
* name and a logout link.
*/
function showAccountInfo() {
FB.api(
{
method: 'fql.query',
query: 'SELECT name,uid,pic_square FROM user WHERE uid='+FB.getSession().uid
},
function(response) {
alert(FB.getSession().uid);
Log.info('API Callback', response);
document.getElementById('account-info').innerHTML = (
'<img src="' + response[0].pic_square + '"> ' +
response[0].name +
' <img onclick="FB.logout()" style="cursor: pointer;"' +
'src="https://s-static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">'
);
}
);
}
/**
* This assumes the user is logged out, and renders a login button.
*/
function showLoginButton() {
document.getElementById('account-info').innerHTML = (
'<img onclick="FB.login()" style="cursor: pointer;"' +
'src="https://s-static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">'
);
}
/**
* This will be called once on page load, and every time the status changes.
*/
function onStatus(response) {
Log.info('onStatus', response);
if (response.session) {
showAccountInfo();
} else {
showLoginButton();
}
}
FB.getLoginStatus(function(response) {
onStatus(response); // once on page load
FB.Event.subscribe('auth.statusChange', onStatus); // every status change
});
</script>
</body>
</html>
我在下面的代码中得到一个"Log is undefined"错误:
function onStatus(response) {
Log.info('onStatus', response);
if (response.session) {
showAccountInfo();
} else {
showLoginButton();
}
}
FB.getLoginStatus(function(response) {
onStatus(response); // once on page load
FB.Event.subscribe('auth.statusChange', onStatus); // every status change
});
你试图利用一个存在于FB的页面,但不是你自己的记录器?
试试下面的代码:
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head></head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '270423476307006',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
FB.getLoginStatus(function(response) {
onStatus(response); // once on page load
FB.Event.subscribe('auth.statusChange', onStatus); // every status change
});
};
(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>
<h1>Login, Logout, Name and Profile Picture</h1>
<div id="account-info"></div>
<script>
/**
* This assumes the user is logged in and renders their profile picture,
* name and a logout link.
*/
function showAccountInfo(resp) {
if( !resp.userID ) {
// we shouldn't be here
return;
}
FB.api(
{
method: 'fql.query',
query: 'SELECT name,uid,pic_square FROM user WHERE uid='+resp.userID
},
function(response) {
document.getElementById('account-info').innerHTML = (
'<img src="' + response[0].pic_square + '"> ' +
response[0].name +
' <img onclick="FB.logout()" style="cursor: pointer;"' +
'src="https://s-static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">'
);
}
);
}
/**
* This assumes the user is logged out, and renders a login button.
*/
function showLoginButton() {
document.getElementById('account-info').innerHTML = (
'<img onclick="FB.login()" style="cursor: pointer;"' +
'src="https://s-static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">'
);
}
/**
* This will be called once on page load, and every time the status changes.
*/
function onStatus(response) {
if (response.authResponse) {
showAccountInfo(response.authResponse);
} else {
showLoginButton();
}
}
</script>
</body>
现在让我们解释一下我们做了什么:
- 我们使用异步加载来确保
FB.getLoginStatus()
只在库成功加载时才会被触发 - 既然你把
oauth
设置为true,那么你需要改变你的代码来处理新的响应(参考这篇文章)(你可能需要改变你的应用程序设置) - 基于#2,在
showAccountInfo()
中使用的当前用户ID将可以使用response.authResponse.userID
访问
相关文章:
- 包括用于facebook评论框的JavaScript SDK
- Google/html5语音识别JavaScript SDK Chrome网络工具包SpeechRecognition
- 在流星上使用微信js-sdk时出现拒绝权限错误
- 如何在corona sdk中从CK编辑器中检索数据
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- FB.login访问令牌facebook javascript SDK
- NativeScript nativeescapet谷歌地图sdk不令人惊叹
- Adobe Air SDK在页面加载时调整窗口大小
- Loopback AngularJS JavaScript SDK - Gruntfile plugin
- 插件SDK中的动态或计算对象创建
- 通过sdk/system/events在修改请求观测器上测试http
- 如果设置了cookie,则Facebook登录Javascript SDK自动登录
- Facebook javascript login sdk, feed undefined
- 将 Facebook fb:login-button 转换为 SDK JavaScript login
- Facebook Login - JavaScript or PHP SDK
- 我应该使用Javascript SDK FB处理代码吗?login POP-UP还是自动处理以获得access_toke
- Facebook SDK for Javascript, FB.login()回调不触发(异步调用永远不会得到响应?)
- Facebook JavaScript SDK Login
- Facebook javascript login to PHP SDK
- Facebook JS SDK返回XSS错误和“user_denied”;响应而不显示授权弹出在FB.Login