【问题标题】:how to get facebook page id inside page fan tab using facebook javascript sdk如何使用 facebook javascript sdk 在页面粉丝选项卡内获取 facebook 页面 id
【发布时间】:2014-02-19 17:19:28
【问题描述】:

我正在尝试创建一个网络应用程序,为 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

我什么都试过了,但没有用:(

谢谢

【问题讨论】:

标签: javascript facebook tabs


【解决方案1】:

为了获取粉丝页面的page id,你需要从Facebook获取signed_request。 https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin 其实这个链接讲的是cavas页面,但是对于fan page的原理是一样的。

但是如果你仔细查看获取这个变量的方法,你会发现,

签名的请求通过 HTTP POST 发送到在应用仪表板中设置为画布 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…}

算法:“HMAC-SHA256” 过期:1404925200 发布时间:1404921078 组oauth_token: “CAAUT5x1Ujq8BAPzh2ze1b4QTBkcZAtRCW6zA1xJszUasqoEPpFRfM1ln3x9pb7mLBujyug5iHUifSnyxmPHOLe030wI3H5DYXubnxbPhww9aipSnwoEr6lwctuQaGKxYvDBdZCNuFiaYIduORTWirmZC2rKL86Fy8fAVEZAyhVaxEaq6ZBw6F4bSFI1s8xtXbBLp7oBFL4wZDZD” 页面:对象 用户:对象 用户 ID:“1519950734891144” 原型:对象

在page对象中,可以找到page id。

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

关于如何解码签名请求,可以看这个链接 https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin 方法一样。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    您可以使用 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);
    

    【讨论】:

      【解决方案3】:

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

      window.top.location;
      

      在 facebook 页面选项卡内不起作用...我以前和现在都这样...

      任何新想法:)

      【讨论】:

        【解决方案4】:

        这是一个经过测试的解决方案,它使用 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>
        

        希望这会有所帮助。干杯。

        【讨论】:

          【解决方案5】:

          您必须将signed_request 从服务器端发布到您的应用程序。 https://developers.facebook.com/docs/pages/tabs 通过链接了解有关页面选项卡和页面 ID 的一些想法。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-07
            • 2011-04-03
            • 2012-01-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多