public string AddFacebookPagesByUrl(string userid, string profileId, string groupId, string name)
        {
            logger.Error(userid + ", " + profileId + ", " + groupId + ", " + name);
            string ret = string.Empty;
            FacebookAccount _FacebookAccount = new FacebookAccount();
            string token = ConfigurationManager.AppSettings["AccessToken1"].ToString();
            try
            {
                #region fancount
                FacebookClient fb = new FacebookClient();
                fb.AccessToken = token;
                int fancountPage = 0;
                try
                {
                    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                    dynamic friends = fb.Get("v2.0/" + profileId);
                    fancountPage = Convert.ToInt32(friends["likes"].ToString());

                }
                catch (Exception)
                {
                    fancountPage = 0;
                    fb.AccessToken = ConfigurationManager.AppSettings["AccessToken2"].ToString();
                    try
                    {
                        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                        dynamic friends = fb.Get("v2.0/" + profileId);
                        fancountPage = Convert.ToInt32(friends["likes"].ToString());
                    }
                    catch (Exception ex)
                    {
                        fancountPage = 0;
                        fb.AccessToken = ConfigurationManager.AppSettings["AccessToken3"].ToString();
                        try
                        {
                            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                            dynamic friends = fb.Get("v2.0/" + profileId);
                            fancountPage = Convert.ToInt32(friends["likes"].ToString());
                        }
                        catch (Exception exx)
                        {
                            fancountPage = 0;
                            fb.AccessToken = ConfigurationManager.AppSettings["AccessToken4"].ToString();
                            try
                            {
                                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                                dynamic friends = fb.Get("v2.0/" + profileId);
                                fancountPage = Convert.ToInt32(friends["likes"].ToString());
                            }
                            catch (Exception exxx)
                            {
                                logger.Error("fancount : " + exxx.Message);
                            }
                        }
                    }
                }
                #endregion


                #region Add FacebookAccount
                objFacebookAccount = new Domain.Socioboard.Domain.FacebookAccount();
                objFacebookAccount.Id = Guid.NewGuid();
                objFacebookAccount.FbUserId = profileId;
                objFacebookAccount.FbUserName = name;
                objFacebookAccount.AccessToken = "";
                objFacebookAccount.Friends = Convert.ToInt32(fancountPage);
                objFacebookAccount.EmailId = "";
                objFacebookAccount.Type = "Page";
                objFacebookAccount.ProfileUrl = "";
                objFacebookAccount.IsActive = 1;
                objFacebookAccount.UserId = Guid.Parse(userid);
                if (!objFacebookAccountRepository.checkFacebookUserExists(objFacebookAccount.FbUserId, objFacebookAccount.UserId))
                {
                    objFacebookAccountRepository.addFacebookUser(objFacebookAccount);
                }
                if (!string.IsNullOrEmpty(objFacebookAccount.FbUserId))
                {
                    ShareathonRepository shreathonpage = new ShareathonRepository();
                    ShareathonGroupRepository objShareathonGroup = new ShareathonGroupRepository();
                    if (shreathonpage.IsShareathonExistFbUserId(objFacebookAccount.UserId, objFacebookAccount.FbUserId))
                    {
                        shreathonpage.UpdateShareathonByFacebookPageId(objFacebookAccount.FbUserId, objFacebookAccount.UserId);
                    }
                    if (objShareathonGroup.IsShareathonExistFbUserId(objFacebookAccount.UserId, objFacebookAccount.FbUserId))
                    {
                        objShareathonGroup.UpdateShareathonByFacebookPageId(objFacebookAccount.FbUserId, objFacebookAccount.UserId);
                    }
                }
                #endregion
                #region SocialProfile
                Domain.Socioboard.Domain.SocialProfile objSocialProfile = new Domain.Socioboard.Domain.SocialProfile();
                objSocialProfile.Id = Guid.NewGuid();
                objSocialProfile.ProfileType = "facebook_page";
                objSocialProfile.ProfileId = profileId;
                objSocialProfile.UserId = Guid.Parse(userid);
                objSocialProfile.ProfileDate = DateTime.Now;
                objSocialProfile.ProfileStatus = 1;
                if (!objSocialProfilesRepository.checkUserProfileExist(objSocialProfile))
                {
                    objSocialProfilesRepository.addNewProfileForUser(objSocialProfile);
                    #region Add TeamMemberProfile
                    Domain.Socioboard.Domain.GroupProfile grpProfile = new Domain.Socioboard.Domain.GroupProfile();
                    grpProfile.Id = Guid.NewGuid();
                    grpProfile.GroupId = Guid.Parse(groupId);
                    grpProfile.GroupOwnerId = objFacebookAccount.UserId;
                    grpProfile.ProfileId = objFacebookAccount.FbUserId;
                    grpProfile.ProfileType = "facebook_page";
                    grpProfile.ProfileName = name;
                    grpProfile.EntryDate = DateTime.UtcNow;
                    grpProfileRepo.AddGroupProfile(grpProfile);
                    #endregion
                }
                #endregion


                try
                {
                    logger.Error(" Token:" + token);
                    if (token != null)
                    {
                        FacebookClient _FacebookClient = new FacebookClient();
                        _FacebookClient.AccessToken = token;
                        dynamic profile = null;

                        try
                        {
                            profile = fb.Get("v2.0/" + profileId);
                            logger.Error("AddFacebookPagesByUrl Token 1");
                        }
                        catch (Exception ex)
                        {
                            try
                            {
                                fb.AccessToken = ConfigurationManager.AppSettings["AccessToken2"].ToString();
                                profile = fb.Get("v2.0/" + profileId);
                            }
                            catch (Exception ex2)
                            {
                                try
                                {
                                    //fb.AccessToken = "CAAKYvwDVmnUBAAR2O9hxFkHzfNG8H6KbQLaiGFMRshJkbttdzhDeprklcb1yaV0rwtC7N8Xz1rsL1cykiRv2ouXtBUFxvOZCNnpFELnQGFV8jGUWjm1GYsZA40IKAORLGoAcSaa2lJkuuSoLBksB8LFPHI4cqW7VVqxgDwZCRwObxqR4Qp9QEDHxa7j1yoZD";
                                    fb.AccessToken = ConfigurationManager.AppSettings["AccessToken3"].ToString();
                                    profile = fb.Get("v2.0/" + profileId);
                                    logger.Error("AddFacebookPagesByUrl Token 3");
                                }
                                catch (Exception ex3)
                                {
                                    try
                                    {
                                        //fb.AccessToken = "CAAKYvwDVmnUBAFtZB8pvVrqYQonmq7MD90oNdoipDc0Te4onP2XlbZAYT4bzOZAKTr8jdhw0P1PclgLOtVxJ9g2qx4vxZAzh2CXqXAZBZAZBwkgWIVjc2B4rcXAp6O5B3gXqd8Ko5ITL9VCZCMOkMZCPc1hBsp0n8zgPt6e3Dd0vaodPBS8nMz7RD";
                                        fb.AccessToken = ConfigurationManager.AppSettings["AccessToken4"].ToString();
                                        profile = fb.Get("v2.0/" + profileId);
                                        logger.Error("AddFacebookPagesByUrl Token 4");
                                    }
                                    catch (Exception ex4)
                                    {
                                        logger.Error("Finally :" + fb.AccessToken);
                                        logger.Error(ex4.Message);

                                    }
                                }
                            }
                        }
                        new Thread(delegate()
                        {
                            AddFbPagePost(userid, fb.AccessToken, profileId);
                        }).Start();
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex.StackTrace);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
            }
            return ret;
        }
        public string AddFacebookPagesInfo(string facebookPage, string userid, string groupId)
        {
            List<Domain.Socioboard.Domain.AddFacebookPage> lstAddFacebookPage = (List<Domain.Socioboard.Domain.AddFacebookPage>)new JavaScriptSerializer().Deserialize(facebookPage, typeof(List<Domain.Socioboard.Domain.AddFacebookPage>));
            foreach (Domain.Socioboard.Domain.AddFacebookPage item in lstAddFacebookPage)
            {
                FacebookClient fb = new FacebookClient();
                fb.AccessToken = item.AccessToken;
                dynamic profile = fb.Get("v2.0/me");
                Domain.Socioboard.Domain.FacebookAccount objFacebookAccount = new Domain.Socioboard.Domain.FacebookAccount();
                objFacebookAccount.FbUserId = item.ProfilePageId;
                objFacebookAccount.FbUserName = item.Name;
                objFacebookAccount.AccessToken = item.AccessToken;
                try
                {
                    objFacebookAccount.Friends = Int32.Parse(item.LikeCount);
                }
                catch (Exception ex)
                {
                    objFacebookAccount.Friends = 0;
                }
                objFacebookAccount.EmailId = item.Email;
                objFacebookAccount.Type = "Page";
                objFacebookAccount.ProfileUrl = "";
                objFacebookAccount.IsActive = 1;
                objFacebookAccount.UserId = Guid.Parse(userid);
                if (!objFacebookAccountRepository.checkFacebookUserExists(objFacebookAccount.FbUserId, objFacebookAccount.UserId))
                {
                    objFacebookAccountRepository.addFacebookUser(objFacebookAccount);
                }
                #region Add TeamMemberProfile
                Domain.Socioboard.Domain.GroupProfile grpProfile = new Domain.Socioboard.Domain.GroupProfile();
                grpProfile.Id = Guid.NewGuid();
                grpProfile.GroupId = Guid.Parse(groupId);
                grpProfile.GroupOwnerId = objFacebookAccount.UserId;
                grpProfile.ProfileId = objFacebookAccount.FbUserId;
                grpProfile.ProfileType = "facebook_page";
                grpProfile.ProfileName = objFacebookAccount.FbUserName;
                grpProfile.EntryDate = DateTime.UtcNow;
                #endregion
                #region SocialProfile
                Domain.Socioboard.Domain.SocialProfile objSocialProfile = new Domain.Socioboard.Domain.SocialProfile();
                objSocialProfile.Id = Guid.NewGuid();
                objSocialProfile.ProfileType = "facebook_page";
                objSocialProfile.ProfileId = item.ProfilePageId;
                objSocialProfile.UserId = Guid.Parse(userid);
                objSocialProfile.ProfileDate = DateTime.Now;
                objSocialProfile.ProfileStatus = 1;
                if (!objSocialProfilesRepository.checkUserProfileExist(objSocialProfile))
                {
                    objSocialProfilesRepository.addNewProfileForUser(objSocialProfile);
                    grpProfileRepo.AddGroupProfile(grpProfile);
                }
                #endregion
                ShareathonRepository shreathonpage = new ShareathonRepository();
                ShareathonGroupRepository objShareathonGroup = new ShareathonGroupRepository();
                if (shreathonpage.IsShareathonExistFbUserId(objFacebookAccount.UserId, objFacebookAccount.FbUserId))
                {
                    shreathonpage.UpdateShareathonByFacebookPageId(objFacebookAccount.FbUserId, objFacebookAccount.UserId);
                }
                if (objShareathonGroup.IsShareathonExistFbUserId(objFacebookAccount.UserId, objFacebookAccount.FbUserId))
                {
                    objShareathonGroup.UpdateShareathonByFacebookPageId(objFacebookAccount.FbUserId, objFacebookAccount.UserId);
                }
                new Thread(delegate()
                {
                    #region Add Facebook Feeds
                    AddFacebookFeeds(userid, fb, profile);
                    #endregion
                    getPageConversations(userid, fb, profile);
                    GetFacebookPageFeed(item.AccessToken, item.ProfilePageId);
                }).Start();

            }
            return "success";
        }