简体   繁体   中英

Catch event if user have already liked any facebook page

I am using following to code to catch event if user have already liked your page but it's not working. It always go to that user has not liked your page.

window.fbAsyncInit = function() { FB.init({ appId : 'app_id', // App ID channelURL : 'path', // Channel File URL status : true, // check login status cookie : true, // enable cookies to allow the server to access the session oauth : true, // enable OAuth 2.0 xfbml : true // parse XFBML });

                FB.getLoginStatus(function(response) {
                    console.log(response);
                    if (response.authResponse) {
                        // logged in and connected user, someone you know
                        var user_id = response.authResponse.userID;
                        var page_id = "1457622007810260";
                        var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+ page_id + " and uid= "+ user_id ;
                        var the_query = FB.Data.query(fql_query);
                        alert(fql_query);
                        alert(user_id);
                        the_query.wait(function(rows) {
                            alert(rows);
                            if (rows.length == 1 && rows[0].uid == user_id) {
                                //user likes the page
                                //do your stuff here
                                alert('likes');
                            } else {
                                // user doesn't like our page yet
                                alert('unlikes');
                            }
                        });
                    } else {
                        // no user session available, someone not known, not logged into fb'
                        alert('no');
                    }
                });
            };

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

and somewhere in html

The user need to grant the user_like permisison. This requires your application to be submitted to and reviewed by Facebook.com

https://developers.facebook.com/docs/facebook-login/permissions/v2.0

If you want to get all the fans of a page, you will need the page_management permission I reckon.

Facebook shows how to do this programmacally on:

https://developers.facebook.com/docs/facebook-login/login-flow-for-web/v2.0

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