找出用户是否喜欢Facebook页面使用Facebook C#SDK

问题描述

| 我正在尝试为Facebook页面构建Facebook“ fangate”标签或“ reveal”标签。 您知道这是怎么回事-当用户访问页面时,如果他们尚未单击“赞”,则会向他们显示一点内容,而当他们单击时,会显示另一内容。 我不是PHP专家,所以我尝试使用Visual Studio 2010中的Facebook C#SDK(http://facebooksdk.codeplex.com)进行此操作。我对.NET也是相当陌生,所以我\做得不好! 我不得不承认我一直在各处粘贴和粘贴代码以使其正常工作,我想我已经快到了,但是我没有收到此错误: 无效的签名请求。 第82行:var DecodedSignedRequest = FacebookSignedRequest.Parse(current,FacebookWebContext.Current.SignedRequest.Data.ToString()); 这是我的代码
        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
        }
所有Facebook设置都在我的web.config文件中,并且我检查了AppID和AppSecret是否正确。 谁能提供我对此问题的任何见解?还有没有发现的更好的方法吗? 非常感谢您的任何帮助。     

解决方法

        好的,我已经解决了-但是我不确定为什么。我觉得Facebook C#SDK会以某种方式解决已签名的请求。如果我使用Request.Forms [\“ signed_request \”]获得签名的请求,那么一切似乎都可以正常工作。 我将分享我的工作代码,以期对其他遇到相同问题的人有所帮助。
        //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\";
       }
    ,        这是我使用的代码,对我来说非常有用。
    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;
        }       
    }