关联 Facebook 应用程序中的 Facebook 粉丝页面 ID

发布于 2024-12-07 16:38:41 字数 202 浏览 6 评论 0原文

我正在开发一个 Facebook 应用程序,需要在加载时获取以下信息。

  • 添加应用程序的 Facebook 粉丝页面 URL / ID。
  • 从特定页面访问此应用程序的用户是否是该页面的管理员。

我正在 ASP.Net 中开发此应用程序,并使用 Facebook Graph API。

非常感谢任何帮助。

I am developing a facebook application which required to get below information while load.

  • Facebook Fan Page URL / Id on which the application has been added.
  • If the user who is accessing this application from specific page is admin of that page or not.

I am developing this application in ASP.Net and I am using Facebook Graph API.

Any help is highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熟人话多 2024-12-14 16:38:41

让我分享一些见解

首先,我强烈建议您使用 Microsoft 的 Facebook C# SDK 而不是使用普通调用在您的 .NET 项目中。


步骤

使用当前的 API,您可以在管理页面中执行此操作

  • 要求用户连接并请求管理页面权限 (manage_page)
  • 以及管理页面的权限,您可以轻松填写​​下拉列表包含用户拥有的所有页面
  • 要求用户将您的应用程序作为选项卡添加到其自己的页面中http://facebook.com/add.php?api_key=[API_KEY]&pages=1&page=[PAGE_ID]

现在您的应用程序已在用户页面上运行,您需要一个检查该页面是否作为选项卡在 Facebook 内部运行以及运行的页面 ID 是什么的方法。

  • 在您指定的“应用程序选项卡网址”页面中,请求 signed_request 并验证Data,因为它具有您需要的["page"]["id"],以便您可以检查您应该保存的Page_Id已保存在当您的用户将您的应用程序添加到其 Facebook 页面时的管理区域。

我希望这有帮助。

代码

登录并请求所有用户页面

<select class="facebook-page-list" disabled="disabled">
    <option>Facebook pages</option></select>

<script>
<!--
  FB.init({
     appId: 'API_KEY',
     cookie: true,
     status: true,
     xfbml: true
  });

  FB.api('/me', function (user) {
     if (user != null) {
        if (user.error) {
           $(".fb-login").show();
        } else {
           // example from Facebook
           var image = document.getElementById('image');
           image.src = 'https://graph.facebook.com/' + user.id + '/picture';
           var name = document.getElementById('name');
           name.innerHTML = user.name

           // get all user Pages
           facebookGetPages();
        }
     }
  });

  function facebookGetPages() {
     FB.getLoginStatus(function (response) {
        if (response.session) {
           access_token = response.session.access_token;
           FB.api(
           {
              method: 'fql.multiquery',
              access_token: access_token,
              queries: {
                 query1: 'select page_id from page_admin where type <> "APPLICATION" and uid = ' + response.session.uid,
                 query2: 'select page_id, name, page_url from page where page_id in (select page_id from #query1)'
              }
           }, function (queries) {
              if (queries.error_msg)
                 alert(queries.error_msg);
              else {
                 pages = queries[1].fql_result_set;

                 $(".facebook-page-list").empty();
                 for (i = 0; i < pages.length; i++)
                    $(".facebook-page-list").append("<option value='" + pages[i].page_id + "'>" + pages[i].name + "</option>");
                 $(".facebook-page-list").attr("disabled", false);
              }
           });
        } else {
           // no user session available, someone you dont know
        }
     });
  }
//--> 
</script>

从您的应用获取页面 ID

ViewBag.signed_request = "can't get id";
dynamic signed_request = FacebookWebContext.Current.SignedRequest;
if(signed_request != null)
{
   ViewBag.signed_request = signed_request.Data.page.id;
}

Let me share some insights

Firstly, I would strongly suggest that you use Microsoft's Facebook C# SDK instead of using plain calls in your .NET project.


Steps

With the current API, you can do this in your ADMIN Page

  • Ask the user to connect and ask for manage pages permission (manage_page)
  • with the permisions to manage the pages, you can easily fill up a dropdown with all pages that the user have
  • Ask the user to add your app to it's own page as a tab using http://facebook.com/add.php?api_key=[API_KEY]&pages=1&page=[PAGE_ID]

Now that you have your app running on the user page, you need a way to check if that page is running inside Facebook as a Tab or not, and what's the Page Id that is running from.

  • In your App Tab Url page that you specified, ask for the signed_request and verify the Data as it has the ["page"]["id"] that you need so you can check against the saves Page_Id that you should have saved on the ADMIN area when your user adds your app to it's facebook page.

I hope this helps.

Code

To login and request all user pages:

<select class="facebook-page-list" disabled="disabled">
    <option>Facebook pages</option></select>

<script>
<!--
  FB.init({
     appId: 'API_KEY',
     cookie: true,
     status: true,
     xfbml: true
  });

  FB.api('/me', function (user) {
     if (user != null) {
        if (user.error) {
           $(".fb-login").show();
        } else {
           // example from Facebook
           var image = document.getElementById('image');
           image.src = 'https://graph.facebook.com/' + user.id + '/picture';
           var name = document.getElementById('name');
           name.innerHTML = user.name

           // get all user Pages
           facebookGetPages();
        }
     }
  });

  function facebookGetPages() {
     FB.getLoginStatus(function (response) {
        if (response.session) {
           access_token = response.session.access_token;
           FB.api(
           {
              method: 'fql.multiquery',
              access_token: access_token,
              queries: {
                 query1: 'select page_id from page_admin where type <> "APPLICATION" and uid = ' + response.session.uid,
                 query2: 'select page_id, name, page_url from page where page_id in (select page_id from #query1)'
              }
           }, function (queries) {
              if (queries.error_msg)
                 alert(queries.error_msg);
              else {
                 pages = queries[1].fql_result_set;

                 $(".facebook-page-list").empty();
                 for (i = 0; i < pages.length; i++)
                    $(".facebook-page-list").append("<option value='" + pages[i].page_id + "'>" + pages[i].name + "</option>");
                 $(".facebook-page-list").attr("disabled", false);
              }
           });
        } else {
           // no user session available, someone you dont know
        }
     });
  }
//--> 
</script>

To get the Page ID from your app:

ViewBag.signed_request = "can't get id";
dynamic signed_request = FacebookWebContext.Current.SignedRequest;
if(signed_request != null)
{
   ViewBag.signed_request = signed_request.Data.page.id;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文