2014-02-19 36 views
3

我特林創建的Facebook頁面創建一個風扇選項卡的web應用程序...如何使用Facebook的JavaScript SDK

我的問題是讓頁扇標籤內的Facebook頁面ID ...

我試過這個: -

FB.api(
"/{page-id}/feed", 
function (response) { 
    if (response && !response.error) { 
    /* handle the result */ 
    } 
} 

);

如何使用facebook的頁面粉絲選項卡獲取Facebook頁面id javascript sdk?

這是圖像描繪了我想做的事: http://www.wtttc.com/image.php?id=25

我什麼都試過,但沒有使用:(

謝謝

+0

看看下面的代碼片段。 http://stackoverflow.com/questions/21887687/how-to-get-facebook-page-id-inside-page-fan-tab-using-facebook-javascript-sdk/23833163#23833163 – brainondev

回答

0

您可以使用FQL得到你想要的一切
我想這會適合你:

var currentPageID; 
var url = window.top.location; 
FB.api(
    { 
     method: 'fql.query', 
     query: "SELECT id FROM object_url WHERE url = '" + url + "'" 
    }, 
    function(response) { 
     currentPageID = response[0].id; 
    } 
); 
alert(currentPageID); 
0

這是一個使用Facebook Javascript SDK及其未公開的parseSignedRequest方法的測試解決方案。

注意:你將不得不使用一些服務器代碼,這個例子是PHP的,但它是最容易找到的。

你的HTML頁面(或任何你服務):

<head> 
..... 
<script type="text/javascript"> 
    // set a variable with the signed_request sent to your app URL by Facebook via POST 
    var signedRequest = "<?php echo $_REQUEST["signed_request"] ?>"; 

    FB.getLoginStatus(function (response) { 
     // do not use the response.authResponse.signedRequest but the one above instead 
     // and let the javascript SDK parse the good signed_request for you 
     var page = this.parseSignedRequest(signedRequest).page; 
     // is the current user an admin of this page? true/false 
     var isAdmin = page.admin; 
     // do you like this page? true/false 
     var isLiked = page.liked; 
     // and here is the Facebook page ID 
     var pageID = page.id; 
     if (response.status === 'connected') { 
      // user is connected 

     } else if (response.status === 'not_authorized') { 
      // the user is logged in to Facebook, 
      // but has not authenticated your app 

     } else { 
      // the user isn't logged in to Facebook. 

     } 

    }, true); 
<script> 

希望這有助於。乾杯。

1

爲了獲得粉絲頁面的頁面ID,您需要從Facebook獲取signed_request。 https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin 事實上,這個鏈接講的是cavas頁面,但它對於粉絲頁面來說是相同的原理。

但是,如果你仔細看得到這個變量的方式,你可以發現,

簽名的請求是通過HTTP POST將其設置爲在App儀表盤的畫布上的網址發送。

這意味着,如果你想獲得簽名的請求數據,你應該通過HTTP POST獲取它。 要獲得粉絲頁面的頁面ID是不可能的,只是使用JavaScript。 Javascript是客戶端語言,因此您無法訪問POST數據。 你需要做的只是把你的JavaScript代碼放入.jsp/.php/...或任何其他服務器端語言頁面。通過服務器端語言頁面,您可以獲得已簽名的請求並將其傳遞給您的JavaScript代碼。 這裏是JSP示例:

<%String signedRequest = request.getParameter("signed_request");%><script>window.signedRequest = "<%=signedRequest%>"</script> 

而且在你的JavaScript,只是解碼你得到的字符串,它包含的頁面ID。

var signedRequest = global.signedRequest; 
     var data1 = signedRequest.split('.')[1]; 
     data1 = JSON.parse(base64decode(data1)); 
     console.log(data1); 

然後你就可以得到的數據是這樣的:

Object {algorithm: "HMAC-SHA256", expires: 1404925200, issued_at: 1404921078, oauth_token: "CAAUT5x1Ujq8BAPzh2ze1b4QTBkcZAtRCW6zA1xJszUasqoEPp…Fy8fAVEZAyhVaxEaq6ZBw6F4bSFI1s8xtXbBLp7oBFL4wZDZD", page: Object…} 

算法: 「HMAC-SHA256」 到期:1404925200 issued_at:1404921078 的oauth_token: 「CAAUT5x1Ujq8BAPzh2ze1b4QTBkcZAtRCW6zA1xJszUasqoEPpFRfM1ln3x9pb7mLBujyug5iHUifSnyxmPHOLe030wI3H5DYXubnxbPhww9aipSnwoEr6lwctuQaGKxYvDBdZCNuFiaYIduORTWirmZC2rKL86Fy8fAVEZAyhVaxEaq6ZBw6F4bSFI1s8xtXbBLp7oBFL4wZDZD」 頁:對象 用戶:對象 user_id:「1519950734891144」 原型:對象

在頁面對象中,可以找到頁面標識。

Object {id: "1522695611287219", liked: true, admin: true} 

關於如何將簽名的請求進行解碼,就可以看到這個鏈接 https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin 這是同樣的方式。

希望這可以幫助你。

相關問題