如何使用facebook javascript sdk在页面粉丝选项卡中获取Facebook页面ID

时间:2014-02-19 17:19:28

标签: javascript facebook tabs

我想创建一个为Facebook页面创建粉丝标签的网络应用程序......

我的问题是......

我试过了: -

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

);

如何使用facebook javascript sdk在页面粉丝选项卡中获取Facebook页面ID?

这是描述我想要做的事情的图像: http://www.wtttc.com/image.php?id=25

我尝试了一切但没有用:(

谢谢

5 个答案:

答案 0 :(得分:1)

要获取粉丝页面的页面ID,您需要从Facebook获取signed_request。 https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin 事实上,这个链接谈论了cavas页面,但它与粉丝页面的原理相同。

但如果仔细查看获取此变量的方法,您可以找到

  

已签名的请求通过HTTP POST发送到在App Dashboard中设置为Canvas URL的URL。

这意味着,如果您想获取已签名的请求数据,则应通过HTTP POST获取。 仅使用javascript无法获取粉丝页面的页面ID。 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…}

算法:&#34; HMAC-SHA256&#34; 到期日:1404925200 issued_at:1404921078 组oauth_token:&#34; CAAUT5x1Ujq8BAPzh2ze1b4QTBkcZAtRCW6zA1xJszUasqoEPpFRfM1ln3x9pb7mLBujyug5iHUifSnyxmPHOLe030wI3H5DYXubnxbPhww9aipSnwoEr6lwctuQaGKxYvDBdZCNuFiaYIduORTWirmZC2rKL86Fy8fAVEZAyhVaxEaq6ZBw6F4bSFI1s8xtXbBLp7oBFL4wZDZD&#34; page:对象 user:对象 user_id:&#34; 1519950734891144&#34; proto :对象

在页面对象中,您可以找到页面ID。

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

关于如何解码已签名的请求,您可以看到此链接 https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin 它的方式是一样的。

希望这可以帮到你。

答案 1 :(得分:0)

您可以使用fql来获取所需的一切。
https://developers.facebook.com/docs/reference/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);

答案 2 :(得分:0)

http://www.wtttc.com/image.php?id=25

window.top.location;

在Facebook页面标签内不起作用...我之前和现在都试过......

任何新想法:)

答案 3 :(得分: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>

希望这会有所帮助。欢呼声。

答案 4 :(得分:0)

您必须从服务器端将signed_request发布到您的应用。 https://developers.facebook.com/docs/pages/tabs 浏览链接,了解页面选项卡和页面ID。