/// <summary> /// Initiate backup of data to OneDrive. /// </summary> private async void BackupToOneDrive() { progressRing.IsActive = true; try { // Request a token to sign in the user var accessToken = await authProvider.GetAccessToken(); // Initialize Graph Client GraphServiceHelper.InitializeClient(authProvider); // Set current user (temp) App.CurrentUser = await GraphServiceHelper.GetMeAsync(); // Find backupFolder in user's OneDrive, if it exists var backupFolder = await GraphServiceHelper.GetOneDriveFolderAsync("Kanban Tasker"); // Create backup folder in OneDrive if not exists if (backupFolder == null) { backupFolder = await GraphServiceHelper.CreateNewOneDriveFolderAsync("Kanban Tasker") as DriveItem; } // Backup datafile (or overwrite) var uploadedFile = await GraphServiceHelper.UploadFileToOneDriveAsync(backupFolder.Id, DataFilename); await DisplayMessageAsync("Data backed up successfully."); var displayName = await GraphServiceHelper.GetMyDisplayNameAsync(); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { txtResults.Text = "Welcome " + displayName; btnSignOutTip.IsEnabled = true; }); } catch (ServiceException ex) { if (ex.StatusCode == System.Net.HttpStatusCode.Unauthorized) { // MS Graph Known Error // Users need to sign into OneDrive at least once // https://docs.microsoft.com/en-us/graph/known-issues#files-onedrive // Empty all cached accounts / data to allow user to rety await authProvider.SignOut(); await DisplayMessageAsync("Error 401. Access Denied. Please make sure you've logged\ninto OneDrive and your email at least once then try again."); } else if (ex.StatusCode == HttpStatusCode.NotFound) { await DisplayMessageAsync("Error 404. Resource requested is not available."); } else if (ex.StatusCode == HttpStatusCode.Conflict) { await DisplayMessageAsync("Error 409. Error backing up, issue retrieving backup folder. Please try again."); } else if (ex.StatusCode == HttpStatusCode.BadGateway) { await DisplayMessageAsync("Error 502. Bad Gateway.\nPlease check your internet connection and try again in a few."); } else if (ex.StatusCode == HttpStatusCode.ServiceUnavailable) { await DisplayMessageAsync("Error 503. Service unavailable due to high load or maintenance.\nPlease try again in a few."); } else if (ex.IsMatch(GraphErrorCode.GeneralException.ToString())) { await DisplayMessageAsync("General Exception. Please check your internet connection and try again in a few."); } } catch (MsalException msalex) { if (msalex.ErrorCode == MsalError.AuthenticationCanceledError) { await DisplayMessageAsync(msalex.Message); } else if (msalex.ErrorCode == MsalError.InvalidGrantError) { // invalid_grant ErrorCode comes from no consent // for the correct scopes (todo: add interactive retry) await DisplayMessageAsync("Invalid access scopes, please contact the developer."); } } catch (Exception ex) { await DisplayMessageAsync(ex.Message); } finally { progressRing.IsActive = false; } }