How to get the Page Id in my Facebook Application page

試著忘記壹切 提交于 2019-12-04 14:22:58

问题


I have my application is hosted in FaceBook as a tab and I want to get the page ID when my application is being added to be stored in my logic. How can I get the page ID, I know it is stored in the URL but when I tried to get it from the page as a server variable, I am not getting it even my application is configured as iFrame ? But this is a standard way to get the parent URL.

C#:

string t= request.serverVariables("HTTP_REFERER");

//doesn't get FB page url even if your app is configured as iframe ?!! @csharpsdk #facebook devs

Any help ?

Thanks a lot.


回答1:


Here is how I do it:

if (FacebookWebContext.Current.SignedRequest != null)
{
  dynamic data = FacebookWebContext.Current.SignedRequest.Data;
  if (data.page != null)
  {
    var pageId = (String)data.page.id;
    var isUserAdmin = (Boolean)data.page.admin;
    var userLikesPage = (Boolean)data.page.liked;
  }
  else
  {
    // not on a page
  }
}



回答2:


The Page ID is not stored in the URL; it is posted to your page within the signed_request form parameter. See this Facebook developer blog post for more details.

You can use the FacebookSignedRequest.Parse method within the Facebook C# SDK to parse the signed request (using your app secret). Once you have done this you can extract the Page ID from the Page JSON object as follows:

string signedRequest = Request.Form["signed_request"];

var DecodedSignedRequest = FacebookSignedRequest.Parse(FacebookContext.Current.AppSecret, SignedRequest);
dynamic SignedRequestData = DecodedSignedRequest.Data;

var RawRequestData = (IDictionary<string, object>)SignedRequestData;

if (RawRequestData.ContainsKey("page") == true)
{
    Facebook.JsonObject RawPageData = (Facebook.JsonObject)RawRequestData["page"];
    if (RawPageData.ContainsKey("id") == true)
         currentFacebookPageID = (string)RawPageData["id"];
}

Hope this helps.




回答3:


Here's the same solution as Andy Sinclairs's in VB that worked for me:

Dim pageId as Int64 = 0
Dim signed_request As String = Request.Form("signed_request")
Dim req = FacebookSignedRequest.Parse(AppSettings("FacebookSecret"), signed_request)
Dim data As IDictionary(Of String, Object) = req.Data
If data.ContainsKey("page") Then
    Dim RawPageData As Facebook.JsonObject = data("page")
    If RawPageData.ContainsKey("id") Then
      pageId = RawPageData("id")
    End If
End If


来源:https://stackoverflow.com/questions/5567250/how-to-get-the-page-id-in-my-facebook-application-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!