From 239f7b12308d3a9c9a7d7a07cdd7ed4264826efc Mon Sep 17 00:00:00 2001 From: KoalaBear Date: Sun, 7 Jun 2020 12:19:11 +0200 Subject: [PATCH] - Added extra help when Google Drive does not initialize correctly --- .../GoogleDriveIndexer.cs | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/OpenDirectoryDownloader.GoogleDrive/GoogleDriveIndexer.cs b/OpenDirectoryDownloader.GoogleDrive/GoogleDriveIndexer.cs index e8b6d6f8..f1277190 100644 --- a/OpenDirectoryDownloader.GoogleDrive/GoogleDriveIndexer.cs +++ b/OpenDirectoryDownloader.GoogleDrive/GoogleDriveIndexer.cs @@ -28,29 +28,38 @@ public static class GoogleDriveIndexer static GoogleDriveIndexer() { - UserCredential credential; - - using (FileStream fileStream = new FileStream("OpenDirectoryDownloader.GoogleDrive.json", FileMode.Open, FileAccess.Read)) + try { - // The file token.json stores the user's access and refresh tokens, and is created - // automatically when the authorization flow completes for the first time. - string credPath = "token.json"; - credential = GoogleWebAuthorizationBroker.AuthorizeAsync( - GoogleClientSecrets.Load(fileStream).Secrets, - Scopes, - "user", - CancellationToken.None, - new FileDataStore(credPath, true)).Result; - - Console.WriteLine($"Credential file saved to: {credPath}"); - } + UserCredential credential; - // Create Drive API service. - DriveService = new DriveService(new BaseClientService.Initializer() + using (FileStream fileStream = new FileStream("OpenDirectoryDownloader.GoogleDrive.json", FileMode.Open, FileAccess.Read)) + { + // The file token.json stores the user's access and refresh tokens, and is created + // automatically when the authorization flow completes for the first time. + string credPath = "token.json"; + credential = GoogleWebAuthorizationBroker.AuthorizeAsync( + GoogleClientSecrets.Load(fileStream).Secrets, + Scopes, + "user", + CancellationToken.None, + new FileDataStore(credPath, true)).Result; + + Console.WriteLine($"Credential file saved to: {credPath}"); + } + + // Create Drive API service. + DriveService = new DriveService(new BaseClientService.Initializer() + { + HttpClientInitializer = credential, + ApplicationName = ApplicationName, + }); + } + catch (Exception ex) { - HttpClientInitializer = credential, - ApplicationName = ApplicationName, - }); + Console.WriteLine($"Error initializing Google Drive, please check OpenDirectoryDownloader.GoogleDrive.json and/or remove the 'token.json' directory. See readme on Github for more help. ERROR: {ex}"); + Logger.Error(ex, "Error initializing Google Drive, please check OpenDirectoryDownloader.GoogleDrive.json and/or remove the 'token.json' directory. See readme on Github for more help."); + throw; + } } public static async Task IndexAsync(WebDirectory webDirectory)