发现如果一个Facebook页面被用户喜爱。使用Facebook C#SDK [英] Finding out if a Facebook page is liked by a user. Using the Facebook C# SDK
问题描述
我想建立一个Facebook的fangate选项卡或显示选项卡中的Facebook页面。
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.
我不是一个PHP的家伙,所以我试图与Facebook的C#SDK(的 http://facebooksdk.codeplex.com )我是相当新的.NET太所以我不会在这方面采取这么好!
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:
无效签名的请求
82行:VAR DecodedSignedRequest = FacebookSignedRequest.Parse(电流,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是正确的。
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,我已经整理出来 - 但我不知道为什么。我有一种感觉,Facebook的C#SDK螺丝周围以某种方式签署的请求。如果我使用Request.Forms [signed_request]这一切似乎工作签名的请求。
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";
}
这篇关于发现如果一个Facebook页面被用户喜爱。使用Facebook C#SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!