简体   繁体   中英

FB.ui feed popup blocked after fb.login

Here's code flow, first is the login and it's work good, but after that i want to immediately call feed function, that's probably the reason why browser blocked it, is there any other way how i can do this? Its work good if the display is set to iframe, but i really want it as a popup. Tahnks.

<a href="#" onclick="fblogin()">Share</a>

function

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 );
                }
            });       
        }
    }); 
} 

You have to call FB.ui dialogs directly on user interaction (= in the callback function of a mouse event). FB.getLoginStatus is asynchronous, that´s why (good) browsers block it.

Use FB.getLoginStatus on page load instead, and store the status. Or better: remove it, because you don´t need to authorized a user for the FB.ui dialogs. And in your code it would not make any sense, because you only use it after FB.login and even after an API call - so the user is surely logged in.

FB.login is asynchronous too, and trying to present a share dialog to the user immediately after login is not only annoying for the user, it´s also against the rules imho. I would count that as "incentivizing":

Only incentivize a person to log into your app, enter a promotion on your app's Page, or check-in at a place. Don't incentivize other actions.

Source: https://developers.facebook.com/policy/

New code:

HTML:

<a href="#" onclick="postToFeed()">Share</a>

JavaScript function "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 );
    }
});   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM