Get all comments on a url created with <fb:comment>

Apr 7, 2011 at 6:40 PM

Althought I have many years of experiance coding with VB and asp.net I'm a newbie to C and the facebook api. I have worked through many of your MVC examples and incorporated them into my asp.net blog but I'm having issues with 2 tasks.

First: How can I get an access token for a user that has already aurthorized my application and visits my site while already logged into facebook?

Second: Currently users can comment on my blogs using facebooks <fb:comment> tag. I want to create the list of comments from code behind and not javascript so my question is how can I use your SDK to query and parse all comments from a posts url in my blog?

Since I'm a newbie the more complete the example the better. Thanks in advance for any and all feedback

Apr 9, 2011 at 7:41 PM
Edited Apr 9, 2011 at 9:19 PM

Here is a working example of what I'm trying to accomplish with a pages comments using the SDK. I actually have several classes that return a dataset for binding but this sample is easier to follow and understand.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// Additional Namespaces/Classes
using System.Web.Services;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;

public partial class SampleGetUrlComments : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // This sample will only work for a single url in the sIds param. FB allows for multiple.
        // This is intened for my non FB connected/logged in users so I'm not fetching a user profile.

        string graphApiBaseURL = "https://graph.facebook.com/comments/?ids=";
        string sIds = "http://developers.facebook.com/docs/reference/fql/comment/";
        string requestURL = graphApiBaseURL + sIds;
        string FBresults = HttpGet(requestURL);

        // using Newtonsoft.Json.Linq. http://json.codeplex.com/
        JObject oData = JObject.Parse(FBresults);
        JArray aData = (JArray)oData[sIds]["data"];

        // Loop outer comments.
        StringBuilder sbComments = new StringBuilder();
        sbComments.Append("<div style='width:440px;font-family:Verdana, Arial;font-size:.7em;'>");
        sbComments.Append("<br /><br /><hr />Comments on: " + sIds + "<hr /><br /><br />");
        sbComments.Append("<ul style='margin-bottom:0px;'>");

        for (int i = 0; i < aData.Count; i++)
        {
            // Check for Likes
            int iLikes;
            int.TryParse(aData[i]["likes"] == null ? "0" : aData[i]["likes"].ToString(), out iLikes);

            // parse out the top level comment
            DateTime dDateStamp;
            if (DateTime.TryParse(aData[i]["created_time"].ToString(), out dDateStamp))
            {
                //we have a date
            }
            else
            {
                dDateStamp = DateTime.Today;
            }
            string sPostID = aData[i]["id"].ToString();
            string sName = aData[i]["from"]["name"].ToString();
            string sUserID = aData[i]["from"]["id"].ToString();
            string sMsg = aData[i]["message"].ToString();

            // Add outer comment
            sbComments.Append("<li style='margin:20px 0 0 0'>");
            sbComments.Append("<div style='display:block;width:100%;height:1px;'><span style='display:inline;float:left;'>" + sName + "</span><span style='display:inline;float:right;margin-right:10px;color:gray;font-size:.8em'>" + dDateStamp.ToString("MMM dd, yyyy @ hh:mm tt") + "</span></div>");
            sbComments.Append("<hr />");
            sbComments.Append("<p style='margin-top:0px;'>" + sMsg + "</p>");
            sbComments.Append("</li>");


            // Check for comments on outer comment, Only going one level deep in this sample.
            int subComments;             // aData[0]["comments"]["count"] == null ? "0" : aData[0]["comments"]["count"].ToString()
            if (int.TryParse(aData[i]["comments"] == null ? "0" : aData[i]["comments"]["count"].ToString(), out subComments))
            {

                if (subComments > 0)
                {
                    //sbComments.Append("<div id='spacer10H' style='height:1px;display:block;'>&nbsp;</div>");
                    sbComments.Append("<ul style='margin:0 0 0 25px;'>");
                    JArray aData2 = (JArray)oData[sIds]["data"][i]["comments"]["data"];
                    // Loop sub comments
                    for (int x = 0; x < aData2.Count; x++)
                    {
                        // Parse out sub comment
                        if (DateTime.TryParse(aData2[x]["created_time"].ToString(), out dDateStamp))
                        {
                            //we have a date
                        }
                        else
                        {
                            dDateStamp = DateTime.Today;
                        }
                        sPostID = aData2[x]["id"].ToString();
                        sName = aData2[x]["from"]["name"].ToString();
                        sUserID = aData2[x]["from"]["id"].ToString();
                        sMsg = aData2[x]["message"].ToString();
                        // Add sub comment
                        sbComments.Append("<li style='margin:15px 0 0 0';>");
                        sbComments.Append("<div style='display:block;width:100%;height:1px;'><span style='display:inline;float:left;'>" + sName + "</span><span class='commentDate' style='display:inline;float:right;margin-right:10px;color:gray;font-size:.8em'>" + dDateStamp.ToString("MMM dd, yyyy @ hh:mm tt") + "</span></div>");
                        sbComments.Append("<hr />");
                        sbComments.Append("<p style='margin-top:0px;'>" + sMsg + "</p>");
                        sbComments.Append("</li>");
                    }
                    // Close sub comments <ul>
                    sbComments.Append("</ul>");
                }
            }

        }
        // Close outer comment <ul>
        sbComments.Append("</ul>");
        // Close disconnected container div
        sbComments.Append("</div>");

        // output html
        Response.Write(sbComments.ToString());
    }


    public string HttpGet(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.KeepAlive = false;
        request.Method = "GET";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream());
        return sr.ReadToEnd();
    }

}
THANKS....
Coordinator
Apr 10, 2011 at 2:40 PM
Facesharp could certainly help in this scenario.

With FaceSharp you can use a custom binder to bind directly to your concrete class or use dynamics.(see the code in the advancedcontroller.cs for a good example of dynamics)

Let me know which direction you would like to take and I might be able to point you in the right direction.

On Sat, Apr 9, 2011 at 3:41 PM, NLandeau <notifications@codeplex.com> wrote:

From: NLandeau

Here is a working example of what I'm trying to accomplish with a pages comments using the SDK. I actually have several classes that return a dataset for binding but this sample is easier to follow and understand.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// Additional Namespaces/Classes
using System.Web.Services;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;

public partial class SampleGetUrlComments : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // This sample will only work for a single url in the sIds param. FB allows for multiple.
        // This is intened for my non FB connected/logged in users so I'm not fetching a user profile.

        string graphApiBaseURL = "https://graph.facebook.com/comments/?ids=";
        string sIds = "http://developers.facebook.com/docs/reference/fql/comment/";
        string requestURL = graphApiBaseURL + sIds;
        string FBresults = HttpGet(requestURL);

        // using Newtonsoft.Json.Linq. www.json.net
        JObject oData = JObject.Parse(FBresults);
        JArray aData = (JArray)oData[sIds]["data"];

        // Loop outer comments.
        StringBuilder sbComments = new StringBuilder();
        sbComments.Append("
"); sbComments.Append("


Comments on: "
+ sIds + "


"
); sbComments.Append("
    "); for (int i = 0; i < aData.Count; i++) { // Check for Likes int iLikes; int.TryParse(aData[i]["likes"] == null ? "0" : aData[i]["likes"].ToString(), out iLikes); // parse out the top level comment DateTime dDateStamp; if (DateTime.TryParse(aData[i]["created_time"].ToString(), out dDateStamp)) { //we have a date } else { dDateStamp = DateTime.Today; } string sPostID = aData[i]["id"].ToString(); string sName = aData[i]["from"]["name"].ToString(); string sUserID = aData[i]["from"]["id"].ToString(); string sMsg = aData[i]["message"].ToString(); // Add outer comment sbComments.Append("
  • "); sbComments.Append("
    " + sName + "" + dDateStamp.ToString("MMM dd, yyyy @ hh:mm tt") + "
    "
    ); sbComments.Append("
    "
    ); sbComments.Append("

    " + sMsg + "

    "
    ); sbComments.Append(""); // Check for comments on outer comment, Only going one level deep in this sample. int subComments; // aData[0]["comments"]["count"] == null ? "0" : aData[0]["comments"]["count"].ToString() if (int.TryParse(aData[i]["comments"] == null ? "0" : aData[i]["comments"]["count"].ToString(), out subComments)) { if (subComments > 0) { //sbComments.Append("
    ");
    sbComments.Append("
      "); JArray aData2 = (JArray)oData[sIds]["data"][i]["comments"]["data"]; // Loop sub comments for (int x = 0; x < aData2.Count; x++) { // Parse out sub comment if (DateTime.TryParse(aData2[x]["created_time"].ToString(), out dDateStamp)) { //we have a date } else { dDateStamp = DateTime.Today; } sPostID = aData2[x]["id"].ToString(); sName = aData2[x]["from"]["name"].ToString(); sUserID = aData2[x]["from"]["id"].ToString(); sMsg = aData2[x]["message"].ToString(); // Add sub comment sbComments.Append("
    • "); sbComments.Append("
      " + sName + "" + dDateStamp.ToString("MMM dd, yyyy @ hh:mm tt") + "
      "
      ); sbComments.Append("
      "
      ); sbComments.Append("

      " + sMsg + "

      "
      ); sbComments.Append(""); } // Close sub comments
        sbComments.Append("
      "
      ); } } } // Close outer comment
        sbComments.Append("
      "
      ); // Close disconnected container div sbComments.Append("
"
); // output html Response.Write(sbComments.ToString()); } public string HttpGet(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.KeepAlive = false; request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream()); return sr.ReadToEnd(); } }
THANKS....

Read the full discussion online.

To add a post to this discussion, reply to this email (facesharp@discussions.codeplex.com)

To start a new discussion for this project, email facesharp@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Apr 13, 2011 at 2:09 AM

Unfortunatly it can't be dynamic right now, System.Dynamics is causing us issues with our url rewiting.

In the mean time I have another problem trying to post a new comment. I can't figure out how to in the sdk, nor have I succeded with webrequest either. I can post to a wall or feed with the SDK no issues, Should I start a new discussion for this one.