public async Task<ActionResult> Index(CancellationToken cancellationToken, GmailFormModel email) { //return View(); var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()). AuthorizeAsync(cancellationToken); AuthorizationCodeMvcApp o = new AuthorizationCodeMvcApp(this, new AppFlowMetadata()); if (result.Credential != null) { if (email.to != null) { BaseClientService.Initializer initializer = new BaseClientService.Initializer { HttpClientInitializer = result.Credential, ApplicationName = "ASP.NET MVC Sample5" }; var service = new GmailService(initializer); string fromEmail = "*****@*****.**"; //string fromName = @"AdamMorgan"; string fromName = ""; /// This code ss needed to extract signed in user info (email, display name) var gps = new PlusService(initializer); var me = gps.People.Get("me").Execute(); fromEmail = me.Emails.First().Value; //fromName = me.DisplayName; //////////////////////////////////////// MimeMessage message = new MimeMessage(); message.To.Add(new MailboxAddress("", email.to)); if (email.cc != null) message.Cc.Add(new MailboxAddress("", email.cc)); if (email.bcc != null) message.Bcc.Add(new MailboxAddress("", email.bcc)); if (email.subject != null) message.Subject = email.subject; var addr = new MailboxAddress(fromName, fromEmail); message.From.Add(addr); if (email.body != null) { message.Body = new TextPart("html") { Text = email.body }; } var ms = new MemoryStream(); message.WriteTo(ms); ms.Position = 0; StreamReader sr = new StreamReader(ms); Google.Apis.Gmail.v1.Data.Message msg = new Google.Apis.Gmail.v1.Data.Message(); string rawString = sr.ReadToEnd(); byte[] raw = System.Text.Encoding.UTF8.GetBytes(rawString); msg.Raw = System.Convert.ToBase64String(raw); var res = service.Users.Messages.Send(msg, "me").Execute(); } return View(); } else { string redirectUri = result.RedirectUri; /// Remove port from the redirect URL. /// This action is needed for http://gmailappasp.apphb.com/ application only. /// Remove it if you hosted solution not on the http://gmailappasp.apphb.com/ redirectUri = Regex.Replace(result.RedirectUri, @"(:\d{3,5})", "", RegexOptions.Multiline | RegexOptions.IgnoreCase); /// return new RedirectResult(redirectUri); } }
public virtual ActionResult RequestRefreshToken() { var result = new AuthorizationCodeMvcApp(this, new AppFlowMetadata(SettingService)). AuthorizeAsync(new CancellationTokenSource().Token).WaitAndUnwrapException(); if (result.Credential != null) { if (!string.IsNullOrEmpty(result.Credential.Token.RefreshToken)) { return Content(result.Credential.Token.RefreshToken); } else { return Content("Server didn't return refresh token. To reissue token you should disconnect app from google drive first."); } } else { return new RedirectResult(result.RedirectUri); } }
/// <summary> /// Authorizes the specified user id. /// </summary> /// <param name="controller">The controller.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task{System.Boolean}.</returns> public async Task AuthorizeAsync(Controller controller, CancellationToken cancellationToken) { Authorized = false; if (!UseGoogleCalendar) return; try { using (var appFlowMetadata = new AppFlowMetadata(HttpContext.Current.Request.PhysicalApplicationPath,GoogleCredentialsJson)) { if (!appFlowMetadata.Valid) throw new Exception("AppFlowMetadata is not valid."); var authorizationCodeMvcApp = new AuthorizationCodeMvcApp(controller, appFlowMetadata); AuthorizationCodeWebApp.AuthResult result = await authorizationCodeMvcApp.AuthorizeAsync(cancellationToken); if (result.Credential == null) { NeedRedirectToGoogle = true; RedirectUri = result.RedirectUri; Cookie.SetNewCookie(Cookie.GoogleCredentialsJsonCookieKey, GoogleCredentialsJson, controller.HttpContext.Response); return; } var initializer = new BaseClientService.Initializer { HttpClientInitializer = result.Credential, ApplicationName = Properties.Settings.Default.WebTitle }; _calendarService = new CalendarService(initializer); bool calendarValid = !String.IsNullOrEmpty(GoogleCalendarId) ? _calendarService.CalendarList.List().Execute().Items.Any( cle => cle.Id == GoogleCalendarId) : _calendarService.CalendarList.List().Execute().Items.Any(); if (!calendarValid) throw new Exception("Calendar Id is not valid."); } } catch (Exception e) { Logger.SetLog(e); return; } Authorized = true; }