public async Task <ActionResult> SendMessageSubmit(UserInfo userInfo) { // After Index method renders the View, user clicks Send Mail, which comes in here. EnsureUser(ref userInfo); SendMessageResponse sendMessageResult = new SendMessageResponse(); // Send email using the Microsoft Graph API. var token = Data.GetUserSessionTokenAny(Settings.GetUserAuthStateId(ControllerContext.HttpContext)); if (token.Provider == Settings.AzureADAuthority || token.Provider == Settings.AzureAD2Authority) { sendMessageResult = await GraphApiHelper.SendMessageAsync( token.AccessToken, GenerateEmail(userInfo)); } else if (token.Provider == Settings.GoogleAuthority) { sendMessageResult = await GoogleApiHelper.SendMessageAsync(token.AccessToken, GenerateEmail(userInfo), token.Username); } // Reuse the Index view for messages (sent, not sent, fail) . // Redirect to tell the browser to call the app back via the Index method. return(RedirectToAction(nameof(Index), new RouteValueDictionary(new Dictionary <string, object> { { "Status", sendMessageResult.Status }, { "StatusMessage", sendMessageResult.StatusMessage }, { "Address", userInfo.Address }, }))); }
public static IAuthorizationCodeFlow GoogleAuthorizationCodeFlow() { return(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = GoogleClientSecrets, Scopes = GoogleApiHelper.GetScopes() })); }
public void Initialize() { if (SettingsHelper.GetValue(SettingsValues.UpdateTestData) == "yes") { // Mocked to avoid compilation level errors GoogleApiHelper.ExportTestDataTable(new GoogleApiUser(), "dummy spreadsheet id"); } }
/// <summary> /// Associates a Google Drive account /// </summary> /// <param name="uid"></param> /// <param name="oauth_token"></param> /// <returns></returns> public ActionResult AssociateGoogleDrive(string code) { // this url needs to be absolute, because it's going to Google var callbackUrl = string.Format( "{0}://{1}{2}", this.Url.RequestContext.HttpContext.Request.Url.Scheme, this.Url.RequestContext.HttpContext.Request.Url.Host, this.Url.Action("AssociateGoogleDrive", "GoogleDriveCallback", new { area = (string)null })); var authorizationState = GoogleApiHelper.ExchangeCode(code, null, callbackUrl); var client = new WebClient(); client.Headers["Authorization"] = "OAuth " + authorizationState.AccessToken; var resultBytes = client.DownloadData("https://www.googleapis.com/oauth2/v2/userinfo"); var resultString = Encoding.UTF8.GetString(resultBytes); var result = new JavaScriptSerializer().Deserialize <AccountInfoJsonResult>(resultString); this.db.GoogleUserAccountInfo.AddObject( new GoogleUserAccoutInfo() { AuthenticationCode = code, Email = result.email, Name = result.name, PracticeId = this.DbPractice.Id, PersonId = this.DbUser.Id, RefreshToken = authorizationState.RefreshToken }); this.db.Notifications.AddObject(new Notification { CreatedOn = this.GetUtcNow(), PracticeId = this.DbPractice.Id, UserToId = this.DbUser.Id, Type = NotificationConstants.GOOGLE_DRIVE_ASSOCIATED_NOTIFICATION_TYPE }); this.db.SaveChanges(); return(this.RedirectToAction("Index")); }