Finding out if a Facebook page is liked by a user. Using the Facebook C# SDK

旧巷老猫 提交于 2019-12-03 16:53:31

OK, I've sorted it out - but I'm not sure why. I have a feeling that the Facebook C# SDK screws around with the signed request in some way. If I get the signed request using Request.Forms["signed_request"] it all seems to work.

I'll share my working code in the hope that it will help others with the same problem.

        //Pull in the facebook app settings from the web.config file
        var settings = ConfigurationManager.GetSection("facebookSettings");
        var current = settings as IFacebookApplication;

        //Set up some stuff for later
        string currentFacebookPageID = current.AppId;
        bool currentFacebookPageLiked = false;

       //Get the signed request
       FacebookSignedRequest SignedRequest = FacebookSignedRequest.Parse(current, Request.Form["signed_request"]);
       dynamic SignedRequestData = SignedRequest.Data;

       //extract what we need from the request
       var RawRequestData = (IDictionary<string, object>)SignedRequestData;  

       //Check to see if we've got the data we need
       if (RawRequestData.ContainsKey("page") == true)
       {
           //We do, lets examine it and set the boolean as appropriate
           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"];
       }

       if (currentFacebookPageLiked)
       {
           //Do some stuff for fans
           lblName.Text = "Hi " + result.first_name + " - You are a fan";

       }
       else
       {
           //Do some stuff for non-fans
           lblName.Text = "Hi " + result.first_name + " - please click the like button";
       }

This is the code i used and it worked great for me.

    protected bool IsPageLiked()
    {
        var current = ConfigurationManager.GetSection("facebookSettings") 
                      as IFacebookApplication;
        dynamic signedRequest = FacebookSignedRequest.Parse(current, Request);

        try
        {
            return signedRequest.Data.page.liked;
        }
        catch (Exception)
        {
            return false;
        }       
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!