public static string ShareFeed(string fbAccesstoken, string FeedId, string pageId, string message, string fbUserId,string FbPageName) { string ret = ""; try { ShareathonRepository sharepo = new ShareathonRepository(); Domain.Socioboard.Domain.SharethonPost objshrpost = new Domain.Socioboard.Domain.SharethonPost(); string link = "https://www.facebook.com/" + pageId + "/posts/" + FeedId; FacebookClient fb = new FacebookClient(); fb.AccessToken = fbAccesstoken; var args = new Dictionary<string, object>(); args["message"] = message; args["link"] = link; if (!sharepo.IsPostExist(fbUserId, pageId, FeedId)) { try { dynamic output = fb.Post("v2.0/" + fbUserId + "/feed", args); string feed_id = output["id"].ToString(); objshrpost.Id = Guid.NewGuid(); objshrpost.Facebookaccountid = fbUserId; objshrpost.Facebookpageid = pageId; objshrpost.PostId = FeedId; objshrpost.PostedTime = DateTime.UtcNow; objshrpost.Facebookpagename = FbPageName; sharepo.AddShareathonPost(objshrpost); ret = "success"; } catch (Exception ex) { Console.Write(ex.StackTrace); return ""; } } } catch (Exception ex) { } return ret; }
public string AddFacebookAccount(string code, string UserId, string GroupId) { string ret = string.Empty; string client_id = ConfigurationManager.AppSettings["ClientId"]; string redirect_uri = ConfigurationManager.AppSettings["RedirectUrl"]; string client_secret = ConfigurationManager.AppSettings["ClientSecretKey"]; long friendscount = 0; try { FacebookClient fb = new FacebookClient(); string profileId = string.Empty; Dictionary<string, object> parameters = new Dictionary<string, object>(); parameters.Add("client_id", client_id); parameters.Add("redirect_uri", redirect_uri); parameters.Add("client_secret", client_secret); parameters.Add("code", code); JsonObject fbaccess_token = null; try { System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls; fbaccess_token = (JsonObject)fb.Get("/oauth/access_token", parameters); } catch (Exception ex) { try { fbaccess_token = (JsonObject)fb.Get("/oauth/access_token", parameters); } catch (Exception ex1) { return "issue_access_token"; } } string accessToken = fbaccess_token["access_token"].ToString(); if (accessToken != null) { fb.AccessToken = accessToken; System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls; dynamic profile = fb.Get("v2.0/me?fields=id,name,email"); dynamic friends = fb.Get("v2.0/me/friends"); try { friendscount = Convert.ToInt16(friends["summary"]["total_count"].ToString()); } catch (Exception ex) { friendscount = 0; logger.Error("friendscount >> " + ex.StackTrace); logger.Error("friendscount >> " + friends.ToString()); } if (!objFacebookAccountRepository.checkFacebookUserExists(Convert.ToString(profile["id"]), Guid.Parse(UserId))) { #region Add FacebookAccount objFacebookAccount = new Domain.Socioboard.Domain.FacebookAccount(); objFacebookAccount.Id = Guid.NewGuid(); objFacebookAccount.FbUserId = (Convert.ToString(profile["id"])); objFacebookAccount.FbUserName = (Convert.ToString(profile["name"])); objFacebookAccount.AccessToken = accessToken; objFacebookAccount.Friends = Convert.ToInt16(friendscount); try { objFacebookAccount.EmailId = (Convert.ToString(profile["email"])); } catch { } objFacebookAccount.Type = "account"; try { objFacebookAccount.ProfileUrl = (Convert.ToString(profile["link"])); } catch { } objFacebookAccount.IsActive = 1; objFacebookAccount.UserId = Guid.Parse(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.UpadteShareathonByFacebookUserId(objFacebookAccount.FbUserId, objFacebookAccount.UserId); } if (objShareathonGroup.IsShareathonExistFbUserId(objFacebookAccount.UserId, objFacebookAccount.FbUserId)) { objShareathonGroup.UpadteShareathonByFacebookUserId(objFacebookAccount.FbUserId, objFacebookAccount.UserId); } } #endregion #region Add TeamMemberProfile Domain.Socioboard.Domain.GroupProfile grpProfile = new Domain.Socioboard.Domain.GroupProfile(); grpProfile.Id = Guid.NewGuid(); grpProfile.GroupId = Guid.Parse(GroupId); grpProfile.GroupOwnerId = Guid.Parse(UserId); grpProfile.ProfileId = objFacebookAccount.FbUserId; grpProfile.ProfileType = "facebook"; grpProfile.ProfileName = (Convert.ToString(profile["name"])); grpProfile.EntryDate = DateTime.UtcNow; grpProfile.ProfilePic = "http://graph.facebook.com/" + objFacebookAccount.FbUserId + "/picture?type=small"; #endregion #region SocialProfile Domain.Socioboard.Domain.SocialProfile objSocialProfile = new Domain.Socioboard.Domain.SocialProfile(); objSocialProfile.Id = Guid.NewGuid(); objSocialProfile.ProfileType = "facebook"; objSocialProfile.ProfileId = (Convert.ToString(profile["id"])); objSocialProfile.UserId = Guid.Parse(UserId); objSocialProfile.ProfileDate = DateTime.Now; objSocialProfile.ProfileStatus = 1; #endregion if (!objSocialProfilesRepository.checkUserProfileExist(objSocialProfile)) { objSocialProfilesRepository.addNewProfileForUser(objSocialProfile); grpProfileRepo.AddGroupProfile(grpProfile); } #region Add Facebook Feeds AddFacebookFeeds(UserId, fb, profile); #endregion ret = "Account Added Successfully"; ret = new JavaScriptSerializer().Serialize(objFacebookAccount); } else { objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(Convert.ToString(profile["id"]), Guid.Parse(UserId)); if (objFacebookAccount.IsActive == 2) { objFacebookAccount.IsActive = 1; objFacebookAccount.AccessToken = accessToken; objFacebookAccountRepository.updateFacebookUser(objFacebookAccount); Domain.Socioboard.Domain.SocialProfile objSocialProfile = new Domain.Socioboard.Domain.SocialProfile(); objSocialProfile.Id = Guid.NewGuid(); objSocialProfile.ProfileType = "facebook"; objSocialProfile.ProfileId = objFacebookAccount.FbUserId; objSocialProfile.UserId = Guid.Parse(UserId); objSocialProfile.ProfileDate = DateTime.Now; objSocialProfile.ProfileStatus = 1; objSocialProfilesRepository.updateSocialProfileStatus(objSocialProfile); ShareathonRepository shreathonpage = new ShareathonRepository(); if (shreathonpage.IsShareathonExistFbUserId(objFacebookAccount.UserId, objFacebookAccount.FbUserId)) { shreathonpage.UpadteShareathonByFacebookUserId(objFacebookAccount.FbUserId, objFacebookAccount.UserId); } ret = "Account Updated successfully !"; } else { ret = "Account already Exist !"; } } } //return new JavaScriptSerializer().Serialize(ret); return ret; } catch (Exception ex) { Console.WriteLine(ex.StackTrace); return "Something Went Wrong"; } }
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"; }