2011-03-30 135 views
3

我有以下的JS代碼。Facebook Javascript,如何檢測用戶是否是我的Facebook頁面的粉絲?在我的網站上?

該代碼的目的是首先獲取用戶的Facebook ID,然後使用FQL檢查該ID與我的頁面ID並確保用戶是粉絲。

我遇到的問題是代碼實際工作的唯一時間是如果我用我自己的個人臉譜配置文件登錄。我認爲它是因爲我的個人資料和FB.init appid以某種方式相關聯?

有人可以看看這段代碼,並告訴我我哪裏出錯了嗎?

我的目標再一次是使用JS首先獲取用戶ID(因此他們的縮略圖),然後交叉引用對我自己的Facebook頁面來檢查,看看他們是否是粉絲。如果他們是Facebook粉絲,那麼我可能會給他們一張優惠券或其他東西。

在此先感謝。

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

我不能肯定地說,但我認爲這是一個權限問題。 Facebook可能不希望您能夠在沒有權限的情況下(例如通過應用程序)檢查用戶的頁面。 – n00dle 2011-03-30 22:56:09

+2

希望你不能。這不是你的事情。 – 2011-03-30 22:58:08

+0

不能說FB是否允許這樣做,但是您確定其他人或其他人嘗試啓用控制檯嗎?否則你的'console.log'命令會失敗並停止JS。 – 2011-03-30 22:59:12

回答

1

FB.getLoginStatus檢查用戶是否連接到您的應用程序..不是Facebook。

但是,當用戶沒有連接到你的應用程序中,.status屬性可以告訴你失敗的原因(如果用戶沒有登錄,在所有的,或者只是你的應用程序)。

所以,你應該用結構

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。

+0

明白了,所以我必須確保用戶已經授權我的應用程序,在用戶ID是共享的。似乎他們應該公開的東西,但我可以看到他們爲什麼不。 – user684844 2011-04-02 22:37:00

相關問題