private Table showFeeds(string FeedURL)
        {
            Table mainTable = null;
            int i = 0;

            TableRow tr;
            TableCell tc;
            TableCell tcImage;
            mainTable = new Table();
            mainTable.Width = Unit.Percentage(100);
            mainTable.CellSpacing = 0;
            mainTable.CellPadding = 0;
            int feedsCount = 0;

            JSONObject me = GetFeeds(FeedURL);

            if (me.Dictionary["data"] != null)
            {
                JSONObject[] feedsprev = new JSONObject[1];
                JSONObject[] feeds = me.Dictionary["data"].Array;
                feedsCount = feeds.Length;
                if (ViewState["html"] != null)
                {
                    feedsprev = (JSONObject[])ViewState["html"] as JSONObject[];
                }
                int mergedarraylength = 0;
                if (feeds != null)
                {
                    mergedarraylength = feeds.Length;
                }
                if (feedsprev[0] != null)
                {
                    mergedarraylength += feedsprev.Length;
                }
                JSONObject[] mergedFeeds = new JSONObject[mergedarraylength];

                if (feedsprev[0] != null)
                {
                    feedsprev.CopyTo(mergedFeeds, 0);
                }
                if (feeds != null)
                {
                    if (feedsprev[0] != null)
                    {
                        feeds.CopyTo(mergedFeeds, feedsprev.Length);
                    }
                    else
                    {
                        feeds.CopyTo(mergedFeeds, 0);
                    }
                }

                ViewState["html"] = mergedFeeds;
                foreach (JSONObject feed in mergedFeeds)
                {
                    tr = new TableRow();
                    tr.CssClass = "fbMainRow";

                    if (ShowUserImage)
                    {
                        tcImage = new TableCell();
                        Image image = new Image();
                        image.ImageUrl = string.Format("https://graph.facebook.com/{0}/picture", feed.Dictionary["from"].Dictionary["id"].String);
                        image.CssClass = "fbHeaderImage";
                        tcImage.Width = Unit.Percentage(5);
                        tcImage.Controls.Add(image);
                        tcImage.VerticalAlign = VerticalAlign.Middle;
                        tr.Cells.Add(tcImage);
                    }
                    tc = new TableCell();
                    tc.Controls.Add(ParseFeed(feed, i));
                    tr.Cells.Add(tc);
                    mainTable.Rows.Add(tr);

                    i++;
                }
            }

            if (feedsCount < WallCount)
            {
                ViewState["next"] = "";
            }
            else
            {
                ViewState["next"] = me.Dictionary["paging"].Dictionary["next"].String;
            }

            return mainTable;
        }
        private Table ParseFeed(JSONObject feed, int counter)
        {
            Table feedTable;
            TableRow feedTableRow;
            TableCell feedTableCell;
            HyperLink objHyperLink;
            Table childTable = new Table();
            TableRow childRow = new TableRow();
            TableCell childCell;

            //first table row in main feed table
            feedTable = new Table();
            feedTableRow = new TableRow();
            feedTable.Rows.Add(feedTableRow);

            //first of all see what is the type of this feed
            switch (feed.Dictionary["type"].String)
            {
                case "status":
                    if (feed.Dictionary.ContainsKey("message"))
                    {
                        //first cell and add table of status data
                        feedTableCell = new TableCell();
                        feedTableRow.Cells.Add(feedTableCell);

                        childTable = new Table();
                        childTable.CellPadding = 2;
                        childRow = new TableRow();
                        childTable.Rows.Add(childRow);
                        childCell = new TableCell();
                        childCell.Text = feed.Dictionary["message"].String;
                        childRow.Cells.Add(childCell);
                        feedTableCell.Controls.Add(childTable);
                    }
                    if (feed.Dictionary.ContainsKey("story"))
                    {
                        //first cell and add table of status data
                        feedTableCell = new TableCell();
                        feedTableRow.Cells.Add(feedTableCell);

                        childTable = new Table();
                        childTable.CellPadding = 2;
                        childRow = new TableRow();
                        childTable.Rows.Add(childRow);
                        childCell = new TableCell();
                        childCell.Text = feed.Dictionary["story"].String;
                        childRow.Cells.Add(childCell);
                        feedTableCell.Controls.Add(childTable);
                    }
                    break;
                case "photo":
                case "link":
                case "video":
                    //create a feed table cell and add child table
                    feedTableCell = new TableCell();
                    feedTableRow.Cells.Add(feedTableCell);
                    childTable = new Table();
                    childTable.CellPadding = 2;
                    feedTableCell.Controls.Add(childTable);

                    if (feed.Dictionary.ContainsKey("picture"))
                    {
                        childRow = new TableRow();
                        childTable.Rows.Add(childRow);
                        Image image = new Image();
                        image.ImageUrl = feed.Dictionary["picture"].String;
                        image.Width = Unit.Pixel(90);
                        childCell = new TableCell();
                        childCell.RowSpan = 4;
                        childRow.Cells.Add(childCell);
                        childCell.Controls.Add(image);
                    }

                    if (feed.Dictionary.ContainsKey("message"))
                    {
                        //next row
                        childRow = new TableRow();
                        childTable.Rows.Add(childRow);
                        childCell = new TableCell();
                        childRow.Cells.Add(childCell);

                        //Remove the link from the message
                        string message = feed.Dictionary["message"].String;
                        if (message.ToLower().Contains("http"))
                        {
                            if (feed.Dictionary["link"].String.Contains("?"))
                            {
                                int linkCount = feed.Dictionary["link"].String.Remove(feed.Dictionary["link"].String.IndexOf("http"), feed.Dictionary["link"].String.Length - feed.Dictionary["link"].String.IndexOf("?")).Length;
                                message = message.Remove(message.IndexOf("http"), linkCount);

                            }
                            else
                                message = message.Remove(message.IndexOf("http"), feed.Dictionary["link"].String.Length);
                        }
                        childCell.Text = message;
                    }

                    if (feed.Dictionary.ContainsKey("name"))
                    {
                        //next row
                        childRow = new TableRow();
                        childTable.Rows.Add(childRow);
                        childCell = new TableCell();
                        childRow.Cells.Add(childCell);

                        objHyperLink = new HyperLink();
                        childCell.Controls.Add(objHyperLink);
                        objHyperLink.Text = feed.Dictionary["name"].String;
                        objHyperLink.Target = "_New";
                        if (feed.Dictionary.ContainsKey("link"))
                        {
                            objHyperLink.NavigateUrl = feed.Dictionary["link"].String;
                        }
                    }

                    if (feed.Dictionary.ContainsKey("description"))
                    {
                        //first cell and add table of status data
                        //next row
                        childRow = new TableRow();
                        childTable.Rows.Add(childRow);
                        childCell = new TableCell();
                        childCell.Text = feed.Dictionary["description"].String;
                        childCell.CssClass = "fbWallDescription";
                        childRow.Cells.Add(childCell);
                    }
                    break;
            }

            //second row in main feed table to display the additional information
            feedTableRow = new TableRow();
            feedTable.Rows.Add(feedTableRow);

            //first cell for feed icon
            feedTableCell = new TableCell();
            feedTableRow.Cells.Add(feedTableCell);

            ///now the child table for data
            childTable = new Table();
            childTable.CellPadding = 2;
            childRow = new TableRow();
            childTable.Rows.Add(childRow);
            feedTableCell.Controls.Add(childTable);

            if (feed.Dictionary.ContainsKey("icon"))
            {
                Image image = new Image();
                image.ImageUrl = feed.Dictionary["icon"].String;

                childCell = new TableCell();
                childRow.Cells.Add(childCell);
                childCell.Controls.Add(image);
            }
            else
            {
                System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                image.ImageUrl = ImagePath + "big_transparent.gif";
                image.Width = Unit.Pixel(16);
                image.Height = Unit.Pixel(17);
                image.BorderWidth = Unit.Pixel(0);

                childCell = new TableCell();
                childRow.Cells.Add(childCell);
                childCell.Controls.Add(image);
            }

            if (feed.Dictionary.ContainsKey("created_time"))
            {
                childCell = new TableCell();
                childRow.Cells.Add(childCell);
                childCell.Text = RelativeTime(feed.Dictionary["created_time"].String.ToString());
                childCell.Style.Add("color", "Gray");
            }

            childRow = new TableRow();
            childTable.Rows.Add(childRow);

            //Show Likes info
            if (feed.Dictionary.ContainsKey("likes"))
            {
                childCell = new TableCell();
                childRow.Cells.Add(childCell);
                System.Web.UI.WebControls.Image img_Like = new System.Web.UI.WebControls.Image();
                img_Like.ImageUrl = ImagePath + "Likes.png";
                img_Like.CssClass = "fbLikes";

                childCell.Controls.Add(img_Like);

                //Showing the number of likes
                childCell = new TableCell();
                childRow.Cells.Add(childCell);
                Label lbl_Likes = new Label();
                lbl_Likes.Text = feed.Dictionary["likes"].Dictionary["count"].String + " people";
                lbl_Likes.CssClass = "fbLikes mrgn";

                // get the story id
                string[] fbinfo = feed.Dictionary["id"].String.Split('_');
                lbl_Likes.Attributes.Add("onClick", "javascript:window.open('https://www.facebook.com/" + this.UserID + "/posts/" + fbinfo[1] + "','_newtab');");
                img_Like.Attributes.Add("onClick", "javascript:window.open('https://www.facebook.com/" + this.UserID + "/posts/" + fbinfo[1] + "','_newtab');");

                Label lbl_likeThis = new Label();
                lbl_likeThis.Text = " like this.";

                childCell.Controls.Add(lbl_Likes);
                childCell.Controls.Add(lbl_likeThis);
            }
            else        // Show only like image
            {
                childCell = new TableCell();
                childRow.Cells.Add(childCell);
                System.Web.UI.WebControls.Image img_Like = new System.Web.UI.WebControls.Image();
                img_Like.ImageUrl = ImagePath + "Likes.png";
                img_Like.CssClass = "fbLikes";
                string[] fbinfo = feed.Dictionary["id"].String.Split('_');
                img_Like.Attributes.Add("onClick", "javascript:window.open('https://www.facebook.com/" + this.UserID + "/posts/" + fbinfo[1] + "','_newtab');");

                childCell.Controls.Add(img_Like);
            }

            //show Comments Info
            if (feed.Dictionary.ContainsKey("comments"))
            {
                //Showing Comment image
                childCell = new TableCell();
                childRow.Cells.Add(childCell);
                System.Web.UI.WebControls.Image img_Comment = new System.Web.UI.WebControls.Image();
                img_Comment.ImageUrl = ImagePath + "comments.png";
                img_Comment.CssClass = "fbLikes";

                childCell.Controls.Add(img_Comment);

                //Showing Text
                childCell = new TableCell();
                childRow.Cells.Add(childCell);
                Label lbl_Comment = new Label();
                lbl_Comment.Text = "View all " + feed.Dictionary["comments"].Dictionary["count"].String + " Comments";
                lbl_Comment.CssClass = "fbLikes mrgn";

                // get the story id
                string[] fbinfo = feed.Dictionary["id"].String.Split('_');
                lbl_Comment.Attributes.Add("onClick", "javascript:window.open('https://www.facebook.com/" + this.UserID + "/posts/" + fbinfo[1] + "','_newtab');");
                img_Comment.Attributes.Add("onClick", "javascript:window.open('https://www.facebook.com/" + this.UserID + "/posts/" + fbinfo[1] + "','_newtab');");

                childCell.Controls.Add(lbl_Comment);
            }

            return feedTable;
        }
 private static void RecursiveObjectToString(JSONObject obj,
     StringBuilder sb, int level)
 {
     if (obj.IsDictionary)
     {
         sb.AppendLine();
         RecursiveDictionaryToString(obj, sb, level + 1);
     }
     else if (obj.IsArray)
     {
         foreach (JSONObject o in obj.Array)
         {
             RecursiveObjectToString(o, sb, level);
             sb.AppendLine();
         }
     }
     else // some sort of scalar value
     {
         sb.Append(obj.String);
     }
 }
 private static void RecursiveDictionaryToString(JSONObject obj,
     StringBuilder sb, int level)
 {
     foreach (KeyValuePair<string, JSONObject> kvp in obj.Dictionary)
     {
         sb.Append(' ', level * 2);
         sb.Append(kvp.Key);
         sb.Append(" => ");
         RecursiveObjectToString(kvp.Value, sb, level);
         sb.AppendLine();
     }
 }
        /// <summary>
        /// Recursively constructs this JSONObject 
        /// </summary>
        private static JSONObject Create(object o)
        {
            JSONObject obj = new JSONObject();
            if (o is object[])
            {
                object[] objArray = o as object[];
                obj._arrayData = new JSONObject[objArray.Length];
                for (int i = 0; i < obj._arrayData.Length; ++i)
                {
                    obj._arrayData[i] = Create(objArray[i]);
                }
            }
            else if (o is Dictionary<string, object>)
            {
                obj._dictData = new Dictionary<string, JSONObject>();
                Dictionary<string, object> dict =
                    o as Dictionary<string, object>;
                foreach (string key in dict.Keys)
                {
                    obj._dictData[key] = Create(dict[key]);
                }
            }
            else if (o != null) // o is a scalar
            {
                obj._stringData = o.ToString();
            }

            return obj;
        }
示例#6
0
        void buttonWriteOnWall_Click(object sender, EventArgs e)
        {
            try
            {
                bool   userMemberOfGroup = false;
                string postUrl           = string.Empty;

                string Comment             = textWall.Text.Trim().Replace("\r\n", " "); // variable to get the text to be written on wall
                bool   PostContainsMessage = true;                                      // false if the comment contains only URL
                string Link = CheckForLinkInPost(Comment);                              // To extract the link from the Comment if any (only single URL will be extracted

                // Check if the comment contains only the URL
                if (string.IsNullOrEmpty(Link))
                {
                    PostContainsMessage = string.IsNullOrEmpty(Comment.Replace(Link, "").Trim());
                }

                if (this.PostOnProfile)
                {
                    //postUrl = string.Format("https://graph.facebook.com/me/feed?access_token={0}&message={1}", oAuthToken, textWall.Text.Trim());
                    // If only message is to be posted
                    if (string.IsNullOrEmpty(Link))
                    {
                        postUrl = string.Format("https://graph.facebook.com/me/feed?access_token={0}&message={1}", oAuthToken, Comment);
                    }
                    // If only Lik is to be posted
                    else if (!string.IsNullOrEmpty(Link) && !PostContainsMessage)
                    {
                        postUrl = string.Format("https://graph.facebook.com/me/feed?access_token={0}&link={1}", oAuthToken, Link);
                    }
                    // If comment contains both message and link
                    else if (!string.IsNullOrEmpty(Link) && PostContainsMessage)
                    {
                        postUrl = string.Format("https://graph.facebook.com/me/feed?access_token={0}&link={1}&message={2}", oAuthToken, Link, Comment);
                    }
                }
                else if (this.PostOnGroupWall)      //To Post on the Group wall
                {
                    //get the groups
                    JSONObject group = GetUserGroups(oAuthToken);

                    if (group.Dictionary["data"] != null)
                    {
                        //Check if the user is a group member
                        JSONObject[] userGroups = group.Dictionary["data"].Array;

                        if (userGroups.Length > 0)
                        {
                            foreach (JSONObject userGroup in userGroups)
                            {
                                if (userGroup.Dictionary["id"].String.Equals(this.OAuthPageID))
                                {
                                    //if its in the group form the url for posting the post
                                    userMemberOfGroup = true;
                                    //postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&message={2}", this.OAuthPageID, oAuthToken, textWall.Text.Trim());

                                    // If only message is to be posted
                                    if (string.IsNullOrEmpty(Link))
                                    {
                                        postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&message={2}", this.OAuthPageID, oAuthToken, Comment);
                                    }
                                    // If only Link is to be posted
                                    else if (!string.IsNullOrEmpty(Link) && !PostContainsMessage)
                                    {
                                        postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&Link={2}", this.OAuthPageID, oAuthToken, Link);
                                    }
                                    // If comment contains both message and link
                                    else if (!string.IsNullOrEmpty(Link) && PostContainsMessage)
                                    {
                                        postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&Link={2}&message={3}", this.OAuthPageID, oAuthToken, Link, Comment);
                                    }
                                    break;
                                }
                            }
                            if (!userMemberOfGroup)
                            {
                                LblMessage      = new Label();
                                LblMessage.Text = "You are not the member of the given group";
                                this.Controls.Add(LblMessage);
                            }
                        }
                        else
                        {
                            LblMessage      = new Label();
                            LblMessage.Text = "Either you are not the member of the given group ID OR User groups permission has not been given to this application.In order for this application to post on your group wall as your account, you need to give this application 'Access User Groups' permission.Please go to the following url to grant this permission:" + string.Format("<a target='_blank' href='https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri={1}&scope=user_groups&response_type=token'>https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri={1}&scope=user_groups&response_type=token</a>", this.OAuthClientID, this.OAuthRedirectUrl);
                            this.Controls.Add(LblMessage);
                        }
                    }
                    else
                    {
                        LblMessage      = new Label();
                        LblMessage.Text = "You are not the member of the given group";
                        this.Controls.Add(LblMessage);
                    }
                }
                else
                {
                    if (this.PostAsPage)
                    {
                        JSONObject me = GetUserPages(oAuthToken);
                        if (me.Dictionary["data"] != null)
                        {
                            JSONObject[] userAccounts = me.Dictionary["data"].Array;

                            if (userAccounts.Length > 0)
                            {
                                if (!userAccounts[0].Dictionary.ContainsKey("access_token"))
                                {
                                    LblMessage      = new Label();
                                    LblMessage.Text = "Manage pages permission has not been given to this application.In order for this application to post on your page as your page's account, you need to give this application 'Manage Pages' permission.Please go to the following url to grant this permission:" + string.Format("<a target='_blank' href='https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri={1}&scope=manage_pages&response_type=token'>https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri={1}&scope=manage_pages&response_type=token</a>", this.OAuthClientID, this.OAuthRedirectUrl);
                                    this.Controls.Add(LblMessage);
                                }
                                else
                                {
                                    bool userAccountFound = false;
                                    foreach (JSONObject userAccount in userAccounts)
                                    {
                                        if (userAccount.Dictionary["id"].String.Equals(this.OAuthPageID.Trim()))
                                        {
                                            userAccountFound = true;
                                            //postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&message={2}", this.OAuthPageID.Trim(), userAccount.Dictionary["access_token"].String, textWall.Text.Trim());

                                            // If only message is to be posted
                                            if (string.IsNullOrEmpty(Link))
                                            {
                                                postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&message={2}", this.OAuthPageID.Trim(), userAccount.Dictionary["access_token"].String, Comment);
                                            }
                                            // If only Lik is to be posted
                                            else if (!string.IsNullOrEmpty(Link) && !PostContainsMessage)
                                            {
                                                postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&link={2}", this.OAuthPageID.Trim(), userAccount.Dictionary["access_token"].String, Link);
                                            }
                                            // If comment contains both message and link
                                            else if (!string.IsNullOrEmpty(Link) && PostContainsMessage)
                                            {
                                                postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&message={2}&link={3}", this.OAuthPageID.Trim(), userAccount.Dictionary["access_token"].String, Comment, Link);
                                            }

                                            break;
                                        }
                                    }

                                    if (!userAccountFound)
                                    {
                                        LblMessage      = new Label();
                                        LblMessage.Text = "The given page was not found in the list of pages.Please make sure that this user is the admin of the page that you have specified.";
                                        this.Controls.Add(LblMessage);
                                    }
                                }
                            }
                            else
                            {
                                LblMessage      = new Label();
                                LblMessage.Text = "The given page was not found.";
                                this.Controls.Add(LblMessage);
                            }
                        }
                        else
                        {
                            LblMessage      = new Label();
                            LblMessage.Text = "No pages found for the given account.";
                            this.Controls.Add(LblMessage);
                        }
                    }
                    else
                    {
                        //postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&message={2}", this.OAuthPageID, oAuthToken, textWall.Text.Trim());

                        // If only message is to be posted
                        if (string.IsNullOrEmpty(Link))
                        {
                            postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&message={2}", this.OAuthPageID.Trim(), Comment);
                        }
                        // If only Lik is to be posted
                        else if (!string.IsNullOrEmpty(Link) && !PostContainsMessage)
                        {
                            postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&link={2}", this.OAuthPageID.Trim(), Link);
                        }
                        // If comment contains both message and link
                        else if (!string.IsNullOrEmpty(Link) && PostContainsMessage)
                        {
                            postUrl = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&message={2}&link={3}", this.OAuthPageID.Trim(), Comment, Link);
                        }
                    }
                }

                if (!String.IsNullOrEmpty(postUrl))
                {
                    HttpWebRequest request2 = WebRequest.Create(postUrl) as HttpWebRequest;
                    request2.Method = "post";
                    using (HttpWebResponse response2 = request2.GetResponse() as HttpWebResponse)
                    {
                        StreamReader reader = new StreamReader(response2.GetResponseStream());
                        string       retVal = reader.ReadToEnd();

                        if (!String.IsNullOrEmpty(retVal))
                        {
                            LblMessage = new Label();
                            if (this.PostOnProfile)
                            {
                                LblMessage.Text = "Message successfully posted on wall.";
                            }
                            else if (this.PostOnGroupWall)
                            {
                                LblMessage.Text = "Message successfully posted on your Group wall.";
                            }
                            else
                            {
                                LblMessage.Text = "Message successfully posted on page.";
                            }
                            this.Controls.Add(LblMessage);
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                LblMessage      = new Label();
                LblMessage.Text = "An error occurred while posting on wall:" + Ex.Message;
                this.Controls.Add(LblMessage);
            }
        }