Finding out if the user likes the Facebook page. Using Facebook C # SDK

I am trying to create a Facebook “fangate” tab or an “Open” tab for a Facebook page.

You know how this happens - when a user visits a page, they are shown one bit of content if they have not clicked “How” and one more time.

I'm not a PHP guy, so I'm trying to do it using the Facebook C # SDK ( http://facebooksdk.codeplex.com ) in Visual Studio I'm also pretty new to .NET, so I'm not doing it that well!

I have to admit that I cut and pasted the code on all sides to make it work, and I think I'm almost there, but I am not getting this error:

Invalid signed request.

Line 82: var DecodedSignedRequest = FacebookSignedRequest.Parse (current, FacebookWebContext.Current.SignedRequest.Data.ToString ());

Here is my code:

        var settings = ConfigurationManager.GetSection("facebookSettings");
        var current = settings as IFacebookApplication;

        var DecodedSignedRequest = FacebookSignedRequest.Parse(current, FacebookWebContext.Current.SignedRequest.Data.ToString());
        dynamic SignedRequestData = DecodedSignedRequest.Data;

        var RawRequestData = (IDictionary<string, object>)SignedRequestData;
        string currentFacebookPageID = current.AppId;
        bool currentFacebookPageLiked = false;

        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"];
        }

        if (currentFacebookPageLiked)
        {
            //Do some stuff for fans

        }
        else
        {
            //Do some stuff for non-fans
        }

All the Facebook settings are in my web.config file, and I checked that the AppID and AppSecret are correct.

Can someone suggest me any understanding of this problem? Is there a better way to do this that I haven't found yet?

Thanks so much for any help.

+3
source share
2 answers

Well, I figured it out - but I'm not sure why. I have a feeling that the SDK for Facebook C # is virtual with a signed request in some way. If I get a signed request using Request.Forms ["signed_request"], it all works.

I will 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";
       }
+2

, , .

    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;
        }       
    }
+1

All Articles