我正在尝试为 Facebook 页面构建 Facebook“fangate”选项卡或“reveal”选项卡。
您知道它是如何进行的 - 当用户访问该页面时,如果他们尚未点击“赞”,则会显示一些内容,一旦点击“赞”,就会显示另一部分内容。
我不是 PHP 人员,所以我尝试使用 Facebook C# SDK 来做到这一点(http://facebooksdk.codeplex.com http://facebooksdk.codeplex.com)在 Visual Studio 2010 中。我对 .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";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)