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

自古美人都是妖i 提交于 2019-12-05 01:34:12

问题


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 = 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 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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/6341749/finding-out-if-a-facebook-page-is-liked-by-a-user-using-the-facebook-c-sharp-sd

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