/// <summary> /// Post status /// </summary> /// <param name="model"></param> /// <param name="network"></param> /// <returns></returns> public ResponseModel Post(SocialMessageModel model, SocialMediaEnums.SocialNetwork network) { try { var token = GetActiveTokenOfSocialMedia(network); if (token != null) { object response = null; var authorizeModel = new SocialMediaResponseModel { AppId = token.AppId, AppSecret = token.AppSecret, AccessToken = token.AccessToken, AccessTokenSecret = token.AccessTokenSecret }; switch (network) { case SocialMediaEnums.SocialNetwork.Facebook: response = SocialUtilities.PostFacebookStatus(authorizeModel, model); break; case SocialMediaEnums.SocialNetwork.Twitter: response = SocialUtilities.PostTwitterStatus(authorizeModel, model); break; case SocialMediaEnums.SocialNetwork.LinkedIn: response = SocialUtilities.PostLinkedInStatus(authorizeModel, model); break; } var responseResult = string.Empty; if (response != null) { responseResult = SerializeUtilities.Serialize(response); } return(new ResponseModel { Success = true, Data = responseResult }); } return(new ResponseModel { Success = false, Message = T("SocialMediaToken_Message_ObjectNotFound") }); } catch (Exception exception) { return(new ResponseModel(exception)); } }
/// <summary> /// Get authorize url /// </summary> /// <param name="model"></param> /// <returns></returns> public string GetAuthorizeUrl(SocialMediaToken model) { var callbackUrl = UrlUtilities.GenerateUrl( HttpContext.Current.Request.RequestContext, "SocialMediaTokens", "Callback", new { id = model.Id }, true); var socialMedia = _socialMediaRepository.GetById(model.SocialMediaId); switch (socialMedia.Id.ToEnum <SocialMediaEnums.SocialNetwork>()) { case SocialMediaEnums.SocialNetwork.Facebook: return(SocialUtilities.GetFacebookAuthorizeUrl(model.AppId, callbackUrl, new List <string> { SocialMediaEnums.FacebookPermission.Email.GetEnumName(), SocialMediaEnums.FacebookPermission.PublishActions.GetEnumName(), SocialMediaEnums.FacebookPermission.UserPosts.GetEnumName(), SocialMediaEnums.FacebookPermission.PublicProfile.GetEnumName() })); case SocialMediaEnums.SocialNetwork.Twitter: //Twitter do not need the authorize url return(callbackUrl); case SocialMediaEnums.SocialNetwork.LinkedIn: callbackUrl = UrlUtilities.GenerateUrl( HttpContext.Current.Request.RequestContext, "SocialMediaTokens", "LinkedInCallback", null, true); StateManager.SetSession(EzCMSContants.LinkedInCallbackId, model.Id); return(SocialUtilities.GetLinkedInAuthorizeUrl(model.AppId, callbackUrl)); } return(string.Empty); }
/// <summary> /// Check and update active token status /// </summary> public void CheckAndUpdateTokenStatus() { var tokens = GetAllActiveTokens().ToList(); foreach (var socialMediaToken in tokens) { var authorizeModel = new SocialMediaAuthorizeModel { AppId = socialMediaToken.AppId, AppSecret = socialMediaToken.AppSecret, AccessToken = socialMediaToken.AccessToken, AccessTokenSecret = socialMediaToken.AccessTokenSecret }; var expired = false; switch (socialMediaToken.SocialMediaId.ToEnum <SocialMediaEnums.SocialNetwork>()) { case SocialMediaEnums.SocialNetwork.Facebook: expired = SocialUtilities.IsFacebookTokenExpired(authorizeModel); break; case SocialMediaEnums.SocialNetwork.Twitter: //Currently now the twitter token does not expired break; case SocialMediaEnums.SocialNetwork.LinkedIn: expired = SocialUtilities.IsLinkedInTokenExpired(authorizeModel); break; } if (expired) { socialMediaToken.Status = SocialMediaEnums.TokenStatus.Expired; socialMediaToken.ExpiredDate = null; socialMediaToken.IsDefault = false; Update(socialMediaToken); } } }
/// <summary> /// Get social media feed by token id /// </summary> /// <param name="tokenId"></param> /// <param name="total"></param> /// <returns></returns> public SocialFeedWidget GetFeed(int tokenId, int total) { var token = GetById(tokenId); if (token != null && token.Status == SocialMediaEnums.TokenStatus.Active) { var posts = new List <SocialFeedItemModel>(); switch (token.SocialMediaId.ToEnum <SocialMediaEnums.SocialNetwork>()) { case SocialMediaEnums.SocialNetwork.Facebook: posts = SocialUtilities.GetFacebookPosts(token.AccessToken); break; case SocialMediaEnums.SocialNetwork.Twitter: var authorize = new SocialMediaAuthorizeModel { AppId = token.AppId, AppSecret = token.AppSecret, AccessToken = token.AccessToken, AccessTokenSecret = token.AccessTokenSecret }; posts = SocialUtilities.GetTwitterPosts(authorize, token.FullName); break; case SocialMediaEnums.SocialNetwork.LinkedIn: posts = SocialUtilities.GetLinkedInPosts(token.AccessToken); break; } return(new SocialFeedWidget { Feed = posts }); } return(null); }
/// <summary> /// Get social information /// </summary> /// <param name="id"></param> /// <returns></returns> public ResponseModel SaveSocialMediaTokenInformation(int id) { var socialMediaToken = GetById(id); if (socialMediaToken != null) { var model = new SocialMediaResponseModel { Id = socialMediaToken.Id, AppId = socialMediaToken.AppId, AppSecret = socialMediaToken.AppSecret, FullName = socialMediaToken.FullName, Email = socialMediaToken.Email, AccessToken = socialMediaToken.AccessToken, AccessTokenSecret = socialMediaToken.AccessTokenSecret, Verifier = socialMediaToken.Verifier }; var callbackUrl = UrlUtilities.GenerateUrl( HttpContext.Current.Request.RequestContext, "SocialMediaTokens", "Callback", new { id }, true); try { switch (socialMediaToken.SocialMediaId.ToEnum <SocialMediaEnums.SocialNetwork>()) { case SocialMediaEnums.SocialNetwork.Facebook: model = SocialUtilities.GetFacebookAccountInfo(model, callbackUrl); break; case SocialMediaEnums.SocialNetwork.Twitter: model = SocialUtilities.GetTwitterAccountInfo(model); break; case SocialMediaEnums.SocialNetwork.LinkedIn: //Specific callback url for LinkedIn callbackUrl = UrlUtilities.GenerateUrl( HttpContext.Current.Request.RequestContext, "SocialMediaTokens", "LinkedInCallback", null, true); StateManager.SetSession(EzCMSContants.LinkedInCallbackId, id); model = SocialUtilities.GetLinkedInAccountInfo(model, callbackUrl); break; } return(SaveSocialMediaResponse(model)); } catch (Exception exception) { return(new ResponseModel { Success = false, Message = T("SocialMediaToken_Message_InvalidConfigure"), DetailMessage = exception.BuildErrorMessage() }); } } return(new ResponseModel { Success = false }); }