这是代码流,首先是登录,它的工作很好,但在那之后,我想立即调用feed函数,这可能是浏览器阻止它的原因,有没有其他方法我可以做到这一点?它的工作很好,如果显示设置为框架,但我真的希望它作为一个弹出。Tahnks .
<a href="#" onclick="fblogin()">Share</a>
函数function fblogin() {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', {fields: 'last_name, first_name, gender, email, id'}, function(reslog) {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
postToFeed(reslog.gender);
}
});
});
} else {
//console.log('X');
}
}, {scope:'email'});
}
postToFeed
function postToFeed(gender) {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.ui( {
method: 'feed',
display: 'popup',
[...]
}, function( response ) {
console.log( 'before',response );
if ( response !== null && typeof response.post_id !== 'undefined' ) {
console.log( 'done',response );
}
});
}
});
}
必须在用户交互时直接调用FB.ui
对话框(=在鼠标事件的回调函数中)。FB.getLoginStatus
是异步的,这就是为什么(好的)浏览器会阻止它。
在页面加载时使用FB.getLoginStatus
,并存储状态。或者更好:删除它,因为您不需要为FB.ui
对话框授权用户。在你的代码中,它没有任何意义,因为你只在FB.login
之后使用它,甚至在API调用之后-所以用户肯定是登录的。
FB.login
也是异步的,试图在用户登录后立即呈现一个共享对话框不仅让用户讨厌,而且也违反了规则。我认为这是"激励":
只鼓励人们登录你的应用,进行推广你的应用程序的页面,或者在一个地方签到。不要激励别人行动。
来源:https://developers.facebook.com/policy/
新代码:HTML:<a href="#" onclick="postToFeed()">Share</a>
JavaScript函数"postToFeed":
FB.ui( {
method: 'feed',
display: 'popup',
[...]
}, function( response ) {
console.log( 'before',response );
if ( response !== null && typeof response.post_id !== 'undefined' ) {
console.log( 'done',response );
}
});