开发者

How to get the if the Page is Liked in my Facebook Application page

I'm using the Facebook sdk from codeplex

I've tried the suggestion at How to get the Page Id in my Facebook Application page

which is this:

 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 开发者_JS百科 
  } 
}

But my data.page is null.

Do I need to ask for some extra permissions on the authorization or something?

Also tried this the FQL suggested at http://blog.woodylabs.com/2010/09/facebook-graph-api-is-user-a-fan-of-page-id-using-fql-and-opengraph-to-mimic-pages-isfan/

But I only have an appId not a page ID.

Any ideas?


You do not need any extra permissions, or authorization. The "Liked" information is available in the signed request as per this Facebook blog post. It is posted each and every time the user clicks on your iFrame application on a Page Tab.

I think the step you are missing is the decoding of the signed request (using your Facebook application secret).

You can use the FacebookSignedRequest.Parse method within the Facebook C# SDK to parse the signed request (by applying your Facebook application secret to it). Once you have done this you can extract the "liked" flag from the Page JSON object as follows:

var DecodedSignedRequest = FacebookSignedRequest.Parse(FacebookContext.Current.AppSecret, FacebookWebContext.Current.SignedRequest.Data);
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"];
    if (RawPageData.ContainsKey("liked") == true)
        currentFacebookPageLiked = (bool)RawPageData["liked"];
}

Or you could, as per my other answer, use the singed_request form variable which is posted to your IFrame app by Facebook.

Hope this helps.


Just figured this out, page data in the FacebookWebContext.Current.SignedRequest object is only populated when the Canvas application is viewed as a page tab (so both left and right side panels), when viewing the full app (no left side panel) then no page data exists.


This is how I've got the answer, I be keen to hear if anyone has better suggestions

FacebookWebClient fbApi = new FacebookWebClient(FacebookWebContext.Current.AccessToken);

dynamic peramaters = new ExpandoObject();
peramaters.method = "pages.isFan";
peramaters.page_id = ConfigurationManager.AppSettings["PageId"]; 

dynamic likes = fbApi.Get(peramaters);

if (!(bool)likes) // do stuff
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜