Facebook fan page app

回眸只為那壹抹淺笑 提交于 2019-12-12 18:16:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!