Facebook Javascript,如何检测用户是否是我的 facebook 页面的粉丝?在我的网站上? [英] Facebook Javascript, How to detect if user is a fan of my facebook page? While on my site?

查看:31
本文介绍了Facebook Javascript,如何检测用户是否是我的 facebook 页面的粉丝?在我的网站上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 JS 代码.

I have the following JS code.

代码的目的是首先获取用户的 facebook id,然后使用 FQL 检查该 id 与我的页面 ID 并确保用户是粉丝.

The code's purpose is to first get the users facebook id, and then using FQL check that id against my page ID and make sure the user is a fan.

我遇到的问题是,代码真正起作用的唯一时间是我使用自己的个人 Facebook 个人资料登录.我认为是因为我的个人资料和 FB.init appid 以某种方式联系在一起?

The problem I am running into is that the only time the code actually works is if i login with my own personal facebook profile. I think its because my profile and the FB.init appid are somehow linked?

有人可以看看这段代码并告诉我我哪里出错了吗?

Can someone take a look at this code and show me where I am going wrong?

我的目标再次是使用 JS 首先获取用户 ID(因此是他们的缩略图),然后将其与我自己的 facebook 页面交叉引用以检查他们是否是粉丝.如果他们是facebook的粉丝,那么我可能会给他们一张优惠券什么的.

My goal again is to use JS to first get the users id (thus their thumbnail image), and then cross reference that against my own facebook page to check and see if they are a fan. If they are a facebook fan, then I will probably give them a coupon or something.

提前致谢.

<script src="http://connect.facebook.net/en_US/all.js"></script>
//Connect to facebook with my app id..
FB.init({ 
    appId:'135445813195028', 
    cookie:false, 
    status:true, 
    xfbml:true
});

//Check to see if user is actually CONNECTED???
FB.getLoginStatus(function(response) {
    if (response.session) {
        // USER IS CONNECTED
        FB.api('/me', function(user) {
            if (user != null) {
                var image = document.getElementById('imagez');
                image.src = 'http://graph.facebook.com/' + user.id + '/picture?type=large';
                var name = document.getElementById('name');
                name.innerHTML = user.name;
                FBID = user.id;
                console.log('Facebook ID:' + FBID);
                //assemble an FQL query to see if the guy is a fan of our page...
                myquery = 'SELECT uid FROM page_fan WHERE page_id = 126923080686340 AND uid = ' + FBID;
                console.log('query = ' + myquery); 
                ///Run FQL Query to see if user is a fan
                FB.api({
                    method: 'fql.query',
                    query: myquery
                }, function(resp) {
                    if (resp.length) {
                        var IsFan = true;
                        alert('You are A fan!')
                        console.log('Visitor is a fan');
                        //show coupon code...
                    } else {
                        alert('Signed into facebook, but Not a fan!');
                        var IsFan = false;
                        console.log('Visitor is not a fan');
                        //show like button...
                        //once like is clicked then refresh the page...
                    }
                });
            //END Run FQL Query to see if user is a fan
            }
        });
        //Figure out if they are a fan...
    } else {
        // USER IS NOT CONNECTED
        alert('NO USER session, user is not logged into facebook!!!');
    }
});

推荐答案

FB.getLoginStatus 检查用户是否连接到您的应用程序 .. 而不是 facebook.

The FB.getLoginStatus check to see if the user is connected to your application .. not to facebook.

但是当用户没有连接到您的应用程序时,.status 属性可以告诉您失败的原因(如果用户根本没有登录,或者只是到您的应用).

But when the user is not connected to your application, the .status property can tell you the reason of the fail (if the user is not logged-in at all, or just to your app).

所以你应该使用的结构是

So the structure you should use is

FB.getLoginStatus(function(response) {
        if(response.session) {
            alert('connected to Application');
        } else {
            // no user session available, someone you dont know
            if(response.status == "notConnected") {
                // But user is logged into facebook
                alert("Not connected to Application, but is logged in to Facebook");
            } else {
                // User is not logged into facebook
                  alert('Not logged in to facebook');
            }
        }
    });

但是您无法访问未授权您的应用程序的用户的 ID.
不是通过 Javascript API.

But you cannot access the ID of a user that has not authorized your Application.
Not through Javascript API.

这篇关于Facebook Javascript,如何检测用户是否是我的 facebook 页面的粉丝?在我的网站上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆