diff --git a/FortnoxAPILibrary.Tests/BaseConnectorTests.cs b/FortnoxAPILibrary.Tests/BaseConnectorTests.cs index 8d0ce16a..eb3f9de5 100644 --- a/FortnoxAPILibrary.Tests/BaseConnectorTests.cs +++ b/FortnoxAPILibrary.Tests/BaseConnectorTests.cs @@ -23,7 +23,8 @@ public void Test_Find_ParamsAdded() { var connector = new CustomerConnector { - Search = new CustomerSearch(){ + Search = new CustomerSearch() + { Name = "TestName", City = "TestCity", LastModified = new DateTime(2000, 01, 01, 20, 10, 05), //2000-01-20 20:10:05 @@ -198,5 +199,17 @@ private static int GetNeededPages(int totalSize, int pageSize) { return (int) Math.Ceiling(totalSize / (float) pageSize); } + + [Ignore("Requires new authorization code.")] + [TestMethod] + public void TestAuth() + { + var authorizationCode = "Placeholder"; + var authConnector = new AuthorizationConnector(); + var token = authConnector.GetAccessToken(authorizationCode, TestCredentials.Client_Secret); + + MyAssert.HasNoError(authConnector); + Assert.IsNotNull(token); + } } } diff --git a/FortnoxAPILibrary.Tests/MyAssert.cs b/FortnoxAPILibrary.Tests/MyAssert.cs index f7ce355c..00bb9f9a 100644 --- a/FortnoxAPILibrary.Tests/MyAssert.cs +++ b/FortnoxAPILibrary.Tests/MyAssert.cs @@ -9,7 +9,7 @@ public static class MyAssert /// Checks if the last request was successfull /// /// - public static void HasNoError(IConnector connector) + public static void HasNoError(IBaseConnector connector) { if (connector.HasError) throw new AssertFailedException($"Request failed due to '{connector.Error.Message}'."); diff --git a/FortnoxAPILibrary/BaseClient.cs b/FortnoxAPILibrary/BaseClient.cs index 50f696d6..e2004ee4 100644 --- a/FortnoxAPILibrary/BaseClient.cs +++ b/FortnoxAPILibrary/BaseClient.cs @@ -48,10 +48,13 @@ public BaseClient() UseRateLimiter = ConnectionSettings.UseRateLimiter; } - public async Task SendAsync(HttpRequestMessage request) + public async Task SendAsync(HttpRequestMessage request, bool addAuthHeaders = true) { - request.Headers.Add(AccessTokenHeader, AccessToken); - request.Headers.Add(ClientSecretHeader, ClientSecret); + if (addAuthHeaders) + { + request.Headers.Add(AccessTokenHeader, AccessToken); + request.Headers.Add(ClientSecretHeader, ClientSecret); + } if (UseRateLimiter) await Throttle(); diff --git a/FortnoxAPILibrary/Connectors/AuthorizationConnector.cs b/FortnoxAPILibrary/Connectors/AuthorizationConnector.cs index 27e352e4..04a8e888 100644 --- a/FortnoxAPILibrary/Connectors/AuthorizationConnector.cs +++ b/FortnoxAPILibrary/Connectors/AuthorizationConnector.cs @@ -27,19 +27,19 @@ public string GetAccessToken(string authorizationCode, string clientSecret) public async Task GetAccessTokenAsync(string authorizationCode, string clientSecret) { if (string.IsNullOrEmpty(authorizationCode)) - return string.Empty; + return null; if (string.IsNullOrEmpty(clientSecret)) - return string.Empty; + return null; try { var wr = SetupRequest(BaseUrl, authorizationCode, clientSecret); - using var response = await HttpClient.SendAsync(wr).ConfigureAwait(false); + using var response = await HttpClient.SendAsync(wr, false).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { Error = ErrorHandler.HandleError(response); - return string.Empty; + return null; } var json = response.Content.ReadAsStringAsync().Result; @@ -51,7 +51,7 @@ public async Task GetAccessTokenAsync(string authorizationCode, string c catch (HttpRequestException we) { Error = ErrorHandler.HandleConnectionException(we); - return string.Empty; + return null; } } @@ -60,8 +60,8 @@ private HttpRequestMessage SetupRequest(string requestUriString, string authoriz Error = null; var wr = new HttpRequestMessage(HttpMethod.Get, requestUriString); - wr.Headers.Add("authorization-code", authorizationCode); - wr.Headers.Add("client-secret", clientSecret); + wr.Headers.Add("Authorization-Code", authorizationCode); + wr.Headers.Add("Client-Secret", clientSecret); wr.Headers.Add("Accept", "application/json"); return wr; } diff --git a/FortnoxAPILibrary/FortnoxAPILibrary.csproj b/FortnoxAPILibrary/FortnoxAPILibrary.csproj index 9803d1e9..a60c6f48 100644 --- a/FortnoxAPILibrary/FortnoxAPILibrary.csproj +++ b/FortnoxAPILibrary/FortnoxAPILibrary.csproj @@ -4,7 +4,7 @@ netstandard2.0 8 Fortnox.NET.SDK - 3.5.0 + 3.5.1 Richard Randak Softwerk AB Official .NET SDK for Fortnox API. This package is on behalf of Fortnox AB developed and maintained by Softwerk AB. For more information please visit our repository on Github.