public GmailService GetGmailServiceObject(string emailId) { GmailService gmailService = new GmailService(); Lead411UserInfo Lead411UserInfo = _iAccount.GetUserSessionEmailWise(emailId); if (Lead411UserInfo != null) { var reqType = _iAccount.GetRequestType(emailId); if (Lead411UserInfo.Provider == AccountType.Gmail.ToString()) { var googleValidationResponse = new GoogleValidationResponse(); //Get new access token if (Authentication.IsTokenExpired(Lead411UserInfo.AccessToken)) { googleValidationResponse = (GoogleValidationResponse) Authentication.GetAccessTokenByRefreshToken(Lead411UserInfo.RefreshToken, reqType); //Save new access token to db _iAccount.SaveRefreshToken(Lead411UserInfo.RefreshToken, Lead411UserInfo.AccessToken, Lead411UserInfo.Email, AccountType.Gmail); } else { googleValidationResponse.access_token = Lead411UserInfo.AccessToken; } if (googleValidationResponse.access_token != null) { Lead411UserInfo.AccessToken = googleValidationResponse.access_token; //Get Userinfo var tokeninfo = (Tokeninfo)Authentication.GetTokenInfo(Lead411UserInfo.AccessToken); //Process emails gmailService = (GmailService) Authentication.CreateAutherizationServiceObject(tokeninfo.Scope, tokeninfo.ExpiresIn, tokeninfo.UserId, Lead411UserInfo.AccessToken, Lead411UserInfo.RefreshToken); //Process mails var userCredential = (UserCredential) Authentication.CreateAutherizationObject(tokeninfo.Scope, tokeninfo.ExpiresIn, tokeninfo.UserId, Lead411UserInfo.AccessToken, Lead411UserInfo.RefreshToken); } } } return(gmailService); }
/// <summary> /// Delete contact group created by dot align project. /// </summary> /// <param name="Lead411UserInfo"></param> /// <param name="reqType"></param> public async Task <bool> DeleteContactFolder(Lead411UserInfo Lead411UserInfo, RequestType reqType) { bool result = false; // Retrieving the contact group is required in order to get the Etag. try { GoogleValidationResponse googleValidationResponse = new GoogleValidationResponse(); //Get new access token if (Authentication.IsTokenExpired(Lead411UserInfo.AccessToken)) { googleValidationResponse = (GoogleValidationResponse)Authentication.GetAccessTokenByRefreshToken(Lead411UserInfo.RefreshToken, reqType); //Save new access token to db _iAccount.SaveRefreshToken(Lead411UserInfo.RefreshToken, Lead411UserInfo.AccessToken, Lead411UserInfo.Email, AccountType.Gmail); } else { googleValidationResponse.access_token = Lead411UserInfo.AccessToken; } if (googleValidationResponse.access_token != null) { Lead411UserInfo.AccessToken = googleValidationResponse.access_token; //Get Userinfo Tokeninfo tokeninfo = (Tokeninfo)Authentication.GetUserProfile(Lead411UserInfo.AccessToken); UserCredential userCredential = (UserCredential)Authentication.CreateAutherizationServiceObject(tokeninfo.Scope, tokeninfo.ExpiresIn, tokeninfo.UserId, Lead411UserInfo.AccessToken, Lead411UserInfo.RefreshToken); ContactsRequest contactRequest = (ContactsRequest)Authentication.ContactsRequest(Lead411UserInfo.AccessToken, Lead411UserInfo.RefreshToken); result = await Operation.DeleteContactFolder(contactRequest); } } catch //(GDataVersionConflictException e) { // Etags mismatch: handle the exception. } return(result); }
/// <summary> /// Process single mail on the call of web jobs. /// </summary> /// <param name="emailId"></param> public async Task ProcessSingleEmail(string emailId) { try { Lead411UserInfo Lead411UserInfo = _iAccount.GetUserSessionEmailWise(emailId); if (Lead411UserInfo != null) { var reqType = _iAccount.GetRequestType(emailId); if (Lead411UserInfo.Provider == AccountType.Gmail.ToString()) { var googleValidationResponse = new GoogleValidationResponse(); //Get new access token if (Authentication.IsTokenExpired(Lead411UserInfo.AccessToken)) { googleValidationResponse = (GoogleValidationResponse) Authentication.GetAccessTokenByRefreshToken(Lead411UserInfo.RefreshToken, reqType); //Save new access token to db _iAccount.SaveRefreshToken(Lead411UserInfo.RefreshToken, Lead411UserInfo.AccessToken, Lead411UserInfo.Email, AccountType.Gmail); } else { googleValidationResponse.access_token = Lead411UserInfo.AccessToken; } if (googleValidationResponse.access_token != null) { Lead411UserInfo.AccessToken = googleValidationResponse.access_token; //Get Userinfo var tokeninfo = (Tokeninfo)Authentication.GetTokenInfo(Lead411UserInfo.AccessToken); //Process emails var gmailService = (GmailService) Authentication.CreateAutherizationServiceObject(tokeninfo.Scope, tokeninfo.ExpiresIn, tokeninfo.UserId, Lead411UserInfo.AccessToken, Lead411UserInfo.RefreshToken); //Get lable to process the mails ////List<string> labels = await Operation.GetLabels(gmailService, tokeninfo.UserId); //Process mails var userCredential = (UserCredential) Authentication.CreateAutherizationObject(tokeninfo.Scope, tokeninfo.ExpiresIn, tokeninfo.UserId, Lead411UserInfo.AccessToken, Lead411UserInfo.RefreshToken); var contactRequest = (ContactsRequest) Authentication.ContactsRequest(userCredential.Token.AccessToken, userCredential.Token.RefreshToken); var contactList = (List <Google.Contacts.Contact>) await Operation.GetContacts(contactRequest); var f = contactRequest.GetContacts(); List <string> list = new List <string>(); foreach (Google.Contacts.Contact t in f.Entries) { foreach (var email in t.Emails) { list.Add(email.Address.ToString()); } } //await MailProcess.Process(gmailService, tokeninfo.UserId, labels, // Lead411UserInfo.UserMembershipId, userCredential, contactList, // contactRequest); } else { //Disable account _iAccount.DisableAccount(Lead411UserInfo.UserMembershipId); //Send mail for relogin EmailHelper.SendEmail(Lead411UserInfo.Email, "Your account is inactive, Please login to Lead411 to activate it.", "Lead411: Inactive Notification"); } } } else { } //return true; } catch (Exception) { // return false; } }