试图弄清楚如何使用JavaScript SDK。我是JavaScrip的新手。我能够使用PHP SDK,但想在客户端运行我的东西。 Anyhoo,我真的不了解开发网站上的SDK文档。所以基本代码是:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
我认为我理解......它加载SDK然后在加载后运行FB.init。我不知道在哪里放置所有OAuth的东西,以及如何构建登录请求(即如果没有登录make a按钮,如果你然后返回访问令牌等。
有人可以抓住这个开头部分。我到处搜索,我觉得不太明白。
修改
好的,第二次尝试使用身份验证。没工作:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'XXX', // App ID
channelUrl : 'XXX', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and connected to 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;
FB.api('/me', function(response) {
alert(response.name);
});
} else if (response.status === 'not_authorized') {
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
FB.logout(function(response) {
console.log('Logged out.');
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email'});
} else {
}
})
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
谢谢!
答案 0 :(得分:1)
就在这条线下面,
// Additional initialization code here
是oauth的东西。
我建议先习惯调用FB.getLoginStatus()
。请参阅:http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
编辑
基于上面编辑的问题 这应该在window.fbAsyncInit函数
中完成FB.api('/me', function(response) {
alert(response.name);
});
并且只有在您检查了getLoginStatus()之后才能知道此人是否已登录。