diff --git a/.github/workflows/publish-nuget-Package.yml b/.github/workflows/publish-nuget-Package.yml index 25ef17e..8e4f99a 100644 --- a/.github/workflows/publish-nuget-Package.yml +++ b/.github/workflows/publish-nuget-Package.yml @@ -1,7 +1,7 @@ name: Publish Nuget Package env: - NUGET_PACKAGE_NAME_VERSION: "GmailHelper.1.10.2.nupkg" + NUGET_PACKAGE_NAME_VERSION: "GmailHelper.1.11.0.nupkg" on: workflow_dispatch: diff --git a/CHANGELOG.md b/CHANGELOG.md index df53f8d..cb0ce42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project documented here. ## [Released] +## [1.11.0](https://www.nuget.org/packages/GmailHelper/1.11.0) - 2024-12-22 +### Changed +- GetGmailService() - Additional optional argument 'credentialsPath' to define custom 'credentials.json' file path. + ## [1.10.2](https://www.nuget.org/packages/GmailHelper/1.10.2) - 2024-12-13 ### Changed - MimeKitLite dependency update from ('4.8.0' -> '4.9.0'). diff --git a/GmailAPIHelper.CORE.Tests/GmailTests.cs b/GmailAPIHelper.CORE.Tests/GmailTests.cs index a1e22d3..f4ef821 100644 --- a/GmailAPIHelper.CORE.Tests/GmailTests.cs +++ b/GmailAPIHelper.CORE.Tests/GmailTests.cs @@ -93,14 +93,14 @@ public void Test_GetGmailService_TokenPath_WorkingDirectory() [TestCategory("GMAIL-TESTS-DOTNETCORE")] public void Test_GetGmailService_TokenPath_Custom() { - var credPath = ""; + var tokenPath = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - credPath = Environment.CurrentDirectory + "\\" + "token.json"; + tokenPath = Environment.CurrentDirectory + "\\" + "token.json"; else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - credPath = Environment.CurrentDirectory + "/" + "token.json"; + tokenPath = Environment.CurrentDirectory + "/" + "token.json"; else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - credPath = Environment.CurrentDirectory + "/" + "token.json"; - var service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, credPath); + tokenPath = Environment.CurrentDirectory + "/" + "token.json"; + var service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, tokenPath); Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService)); } @@ -117,6 +117,29 @@ public void Test_GetGmailService_TokenPath_Custom_EmptyPath() catch (ArgumentException) { } } + [TestMethod] + [TestCategory("GMAIL-TESTS-DOTNETCORE")] + public void Test_GetGmailService_CredentialsPath_Custom() + { + var tokenPath = ""; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + tokenPath = Environment.CurrentDirectory + "\\" + "token.json"; + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + tokenPath = Environment.CurrentDirectory + "/" + "token.json"; + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + tokenPath = Environment.CurrentDirectory + "/" + "token.json"; + var credentialsPath = Environment.CurrentDirectory + "/" + "credentials.json"; + // Token Path & Credentials Path + var service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, tokenPath, credentialsPath); + Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService)); + // Only Token Path + service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, tokenPath); + Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService)); + // Only Credentials Path + service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.WORKING_DIRECTORY, credentialsPath: credentialsPath); + Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService)); + } + [TestMethod] [TestCategory("GMAIL-TESTS-DOTNETCORE")] public void Test_GmailService_Dispose() diff --git a/GmailAPIHelper/GmailAPIHelper.csproj b/GmailAPIHelper/GmailAPIHelper.csproj index 7b04d39..e66cf7a 100644 --- a/GmailAPIHelper/GmailAPIHelper.csproj +++ b/GmailAPIHelper/GmailAPIHelper.csproj @@ -30,8 +30,8 @@ Solution Version: GmailHelper git LICENSE - 1. MimeKitLite dependency update from ('4.8.0' -> '4.9.0'). - 1.10.2 + 1. GetGmailService() - Additional optional argument 'credentialsPath' to define custom 'credentials.json' file path. + 1.11.0 diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 85cbec6..ca2133b 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -75,40 +75,54 @@ public enum MessageListVisibility } /// - /// Sets the credentials path to be used. + /// Sets the token file path to be used. /// /// 'TokenPathType' enum value. 'HOME' for users home directory, 'WORKING_DIRECTORY' for working directory, 'CUSTOM' for any other custom path to be used. - /// Token path value in case of 'TokenPathType - CUSTOM' value. - /// Credentials file path. + /// Token file path value in case of 'TokenPathType - CUSTOM' value. + /// Token file path. /// Throws - 'NotImplementedException' for OS Platforms other than Windows/Linux/OSX. - private static string SetCredentialPath(TokenPathType tokenPathType, string tokenPath = "") + private static string SetTokenPath(TokenPathType tokenPathType, string tokenPath = "") { - string credPath = ""; + string filePath = ""; if (tokenPathType == TokenPathType.HOME) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - credPath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\" + _tokenFile; + filePath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\" + _tokenFile; else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - credPath = Environment.GetEnvironmentVariable("HOME") + "/" + _tokenFile; + filePath = Environment.GetEnvironmentVariable("HOME") + "/" + _tokenFile; else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - credPath = Environment.GetEnvironmentVariable("HOME") + "/" + _tokenFile; + filePath = Environment.GetEnvironmentVariable("HOME") + "/" + _tokenFile; else throw new NotImplementedException("OS Platform: Not 'Windows/Linux/OSX' Platform."); } else if (tokenPathType == TokenPathType.WORKING_DIRECTORY) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - credPath = Environment.CurrentDirectory + "\\" + _tokenFile; + filePath = Environment.CurrentDirectory + "\\" + _tokenFile; else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - credPath = Environment.CurrentDirectory + "/" + _tokenFile; + filePath = Environment.CurrentDirectory + "/" + _tokenFile; else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - credPath = Environment.CurrentDirectory + "/" + _tokenFile; + filePath = Environment.CurrentDirectory + "/" + _tokenFile; else throw new NotImplementedException("OS Platform: Not 'Windows/Linux/OSX' Platform."); } else if (tokenPathType == TokenPathType.CUSTOM) - credPath = tokenPath; - return credPath; + filePath = tokenPath; + return filePath; + } + + /// + /// Sets the 'credentials.json' file path to be used. + /// + /// 'credentials.json' file path. + /// 'credentials.json' file path. + private static string SetCredentialsPath(string credentialsPath) + { + string file = "credentials.json"; + if (string.IsNullOrEmpty(credentialsPath)) + return file; + else + return credentialsPath; } /// @@ -119,8 +133,9 @@ private static string SetCredentialPath(TokenPathType tokenPathType, string toke /// Default value - 'WORKING_DIRECTORY'. /// 'token.json' path to save generated token from gmail authentication/authorization. /// Always asks in case of change in gmail authentication or valid token file missing in the given path. Default path is blank, required for 'TokenPathType - CUSTOM'. + /// 'credentials.json' file path. Default path is blank in which case uses working directory for 'credentials.json' file lookup. /// Gmail Service. - public static GmailService GetGmailService(string applicationName, TokenPathType tokenPathType = TokenPathType.WORKING_DIRECTORY, string tokenPath = "") + public static GmailService GetGmailService(string applicationName, TokenPathType tokenPathType = TokenPathType.WORKING_DIRECTORY, string tokenPath = "", string credentialsPath = "") { var scopes = new List { @@ -131,10 +146,11 @@ public static GmailService GetGmailService(string applicationName, TokenPathType GmailService.Scope.GmailSend }; UserCredential credential; - using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read)) + var credentials = SetCredentialsPath(credentialsPath); + using (var stream = new FileStream(credentials, 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 = SetCredentialPath(tokenPathType, tokenPath); + string credPath = SetTokenPath(tokenPathType, tokenPath); credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.FromStream(stream).Secrets, scopes,