这个代码一直有效,直到它到达postToWall,它失败了。警报显示登录成功之前。 有趣的是,如果我用FP.ui替换FB.api调用,一切都很好 但我不希望用户输入消息 - 我只想在消息上添加消息 用户的墙。
window.fbAsyncInit = function() {
alert("before FB.init");
FB.init({
appId : '<?=$api_key?>', // App ID
channelUrl : 'http://dev.babynames.com/_assets/facebook/inc/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
});
FBLogin();
};
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
function FBLogin(){
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 + '.');
alert("login succeeded");
postToWall();
});
} else {
console.log('User cancelled login or did not fully authorize.');
alert("login failed");
}
}, {scope: 'publish_stream'});
}
function postToWall(){
alert("In postToWall");
var msgObj =
{
link:'<?=$public_link?>',
name:'Help me pick a name for my baby!',
picture:'http://dev.babynames.com/_assets/img/icn_vote.gif',
description:'I have updated my favorite names list on BabyNames.com'
};
FB.api('/me/feed', 'post', msgObj, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}