开发者

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

I'm trying to build a Facebook 'fangate' tab or 'reveal' tab for a Facebook page.

You know how it goes - when a user visits the page, they are shown one bit of content if they haven't yet clicked 'Like' and another once they have.

I'm not a PHP guy so I'm attempting to do this with the Facebook C# SDK (http://facebooksdk.codeplex.com) in Visual Studio 2010. I'm fairly new to .NET too so I'm not doing so well with this!

I have to admit I've been cutting and pasting code from all over the place to get this to work and I think I'm almost there but I'm not getting this error:

Invalid signed request.

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

Here's my code:

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

        var DecodedSignedRequest = Facebo开发者_JAVA技巧okSignedRequest.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 have checked that the AppID and AppSecret are correct.

Can anyone offer me any insight into this issue please? Is there a better way of doing this that I've not yet found?

Many thanks in advance for any help.


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;
        }       
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜