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

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

问题描述

我有以下的JS code。

在code的目的是首先得到用户的Facebook ID,然后使用FQL检查ID对我的页ID,并确保用户的风扇。

我遇到的问题是,code实际工作唯一的一次是,如果我有我自己的个人Facebook的个人资料登录。我认为它是因为我的个人资料和FB.init的appid有某种联系?

有人可以看看这个code和告诉我在哪里,我错了?

我的目标又是用JS首先得到用户的ID(因而其缩略图),然后交叉引用,对我自己的Facebook页面,检查,看看他们是一个迷。如果他们是一个Facebook粉丝,那么我可能会给予他们的优惠券或东西。

在此先感谢。

 <脚本SRC =htt​​p://connect.facebook.net/en_US/all.js>< / SCRIPT>
//连接到我的应用程序ID的Facebook ..
FB.init({
    APPID:'135445813195028',
    饼干:假的,
    状态:真实,
    XFBML:真
});//检查用户是否实际上连接?
FB.getLoginStatus(功能(响应){
    如果(response.session){
        //用户连接
        FB.api('/我功能(用户){
            如果(用户!= NULL){
                VAR图像=的document.getElementById('imagez');
                image.src ='http://graph.facebook.com/'+ user.id +'?/图像类型=大';
                变种名称=的document.getElementById('名');
                name.innerHTML = user.name;
                FBID = user.id;
                的console.log('Facebook的ID:'+ FBID);
                //组建一个FQL查询,看是否家伙是我们的页面的粉丝...
                更改为MyQuery =SELECT FROM UID WHERE page_fan = PAGE_ID 126923080686340和uid ='+ FBID;
                的console.log('查询='+更改为MyQuery);
                ///运行FQL查询,看看用户是风扇
                FB.api({
                    方法:'fql.query',
                    查询:更改为MyQuery
                },功能(RESP){
                    如果(resp.length){
                        VAR IsFan =真;
                        警报('你是粉丝!)
                        的console.log('观众是帆');
                        //显示优惠券code ...
                    }其他{
                        警报('签名到Facebook的,但不是一个球迷!);
                        VAR IsFan = FALSE;
                        的console.log('观众是不是一个球迷');
                        //显示像按钮...
                        //一旦喜欢被点击然后刷新页面...
                    }
                });
            // END运行FQL查询,看看用户是风扇
            }
        });
        //计算出,如果他们是一个迷...
    }其他{
        //用户没有连接
        警报('的用户会话,用户没有登录到Facebook的!');
    }
});


解决方案

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

但是,当用户没有连接到你的应用程序中, .STATUS 属性可以告诉你的失败(的如果用户没有已登录的原因在所有的,或者只是你的应用程序的)。

所以,你应该用结构

  FB.getLoginStatus(功能(响应){
        如果(response.session){
            警报('连接到应用程序);
        }其他{
            //没有用户会话中可用,一个你不知道
            如果(response.status ==notConnected){
                //但用户登录到Facebook的
                警报(未连接到应用程序,但登录到Facebook的);
            }其他{
                //用户未登录到Facebook的
                  警报('不登录到Facebook');
            }
        }
    });

但你不能访问未授权的应用程序的用户的ID。结果
不通过JavaScript API。

I have the following JS code.

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.

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?

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.

Thanks in advance.

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

解决方案

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

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

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天全站免登陆