查明用户是否喜欢Facebook页面。使用Facebook C#SDK

问题描述:

我正在尝试为Facebook页面构建Facebook'fangate'选项卡或'显示'选项卡。查明用户是否喜欢Facebook页面。使用Facebook C#SDK

你知道它是怎么回事 - 当一个用户访问该页面时,如果他们还没有点击'Like'和另一个内容,他们会显示一个内容位。

我不是一个PHP家伙,所以我试图在Visual Studio 2010中使用Facebook C#SDK(http://facebooksdk.codeplex.com)来做到这一点。我对.NET也很新,所以我没有做的很好。这个!

我不得不承认,我已经从所有剪切和粘贴代码的地方得到这个工作,我觉得我几乎没有,但我没有得到这个错误:

签署无效请求。

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

我使用的是VS 2008,所以不得不使用解决方法来引用NuGet包。而且我没有找到对IFrarchApplication或FacebookSignedRequest的引用。你能指定命名空间吗?谢谢! – wloescher 2013-05-10 17:14:47