问题
I've built a few facebook apps using the c# sdk. and using code below to check if the user has liked the fan page and showing content to reflect that.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["SignedRequest"] != null)
{
signedRequest = Session["SignedRequest"].ToString();
}
else
{
signedRequest = Request.Form["signed_request"];
}
if (!string.IsNullOrEmpty(signedRequest))
{
dynamic SignedRequestData;
var DecodedSignedRequest = FacebookWebContext.Current.SignedRequest.Data;
SignedRequestData = DecodedSignedRequest
var RawRequestData = (IDictionary<string, object>)SignedRequestData;
foreach (KeyValuePair<string, object> paird in RawRequestData)
{
Response.Write("key =" + paird.Key.ToString() + " value =" + paird.Value.ToString() + "<br/>");
}
if (RawRequestData.ContainsKey("page"))
{
Facebook.JsonObject RawPageData = (Facebook.JsonObject)RawRequestData["page"];
if (RawPageData.ContainsKey("liked"))
{
if (bool.Parse(RawPageData["liked"].ToString()))
{
LikedContent.Visible = true;
if (!IsPostBack)
{
PageSetup();
}
}
else
{
UnlikedContent.Visible = true;
}
}
else
{
UnlikedContent.Visible = true;
}
}
else
{
DefaultContent.Visible = true;
}
}
else
{
DefaultContent.Visible = true;
}
}
The problem with this is that if i have multiple pages on the app i get cross domain issues. can fix this quite easily for ie using the p3p header in the gloabl.asac.cs file. the issue however still remains in safari.
Now that the current version 6 of the c# sdk recommends using the facebook javascript sdk to call the open graph and check that the user likes a page and then pass it to the code behind i was wanting to know what actually is the best practice for building facebook fan page tab apps?
When ever i access the users information using the javascript sdk i get a pop up box to authorise the app. i don't want this. is there a way round this?
cheers
回答1:
You can call graph api method like this in code behind
void CallFacebookApi(string oAuthToken, string userId)
{
string userLikeUrl = "https://graph.facebook.com/me/Likes/" + pageId +"?access_token=" + oAuthToken;
response = requestFBData(userLikeUrl);
if (response.Length > 0)
{
JObject userLike = JObject.Parse(response);
int count = userLike["data"].Count();
if (count > 0){
//user liked your page.
}
else {
//user do not liked your page yet
}
}
}
public string requestFBData(string action)
{
string results = string.Empty;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(action);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
results = sr.ReadToEnd();
sr.Close();
}
catch (Exception e)
{
if (e.Message.Contains("400"))
{
//invalid reponse
}
}
return results;
}
来源:https://stackoverflow.com/questions/10721417/facebook-fan-page-app