diff --git a/TeslaSolarCharger.Tests/Services/Server/BackendApiService.cs b/TeslaSolarCharger.Tests/Services/Server/BackendApiService.cs index 32c12ea0f..0b5556d90 100644 --- a/TeslaSolarCharger.Tests/Services/Server/BackendApiService.cs +++ b/TeslaSolarCharger.Tests/Services/Server/BackendApiService.cs @@ -25,7 +25,7 @@ public void CanEnCodeCorrectUrl() State = "8774fbe7-f9aa-4e36-8e88-5c8b27137f20", }; var url = backendApiService.GenerateAuthUrl(requestInformation, "en-US"); - var expectedUrl = "https://auth.tesla.com/oauth2/v3/authorize?&client_id=f29f71d6285a-4873-8b6b-80f15854892e&locale=en-US&prompt=login&redirect_uri=https%3A%2F%2Fwww.teslasolarcharger.de%2F&response_type=code&scope=offline_access%20vehicle_device_data%20vehicle_cmds%20vehicle_charging_cmds&state=8774fbe7-f9aa-4e36-8e88-5c8b27137f20"; + var expectedUrl = "https://auth.tesla.com/oauth2/v3/authorize?&client_id=f29f71d6285a-4873-8b6b-80f15854892e&locale=en-US&prompt=login&redirect_uri=https%3A%2F%2Fwww.teslasolarcharger.de%2F&response_type=code&scope=offline_access%20vehicle_device_data%20vehicle_cmds%20vehicle_charging_cmds&state=8774fbe7-f9aa-4e36-8e88-5c8b27137f20&prompt_missing_scopes=true"; Assert.Equal(expectedUrl, url); } } diff --git a/TeslaSolarCharger/Server/Program.cs b/TeslaSolarCharger/Server/Program.cs index 2a3142bdd..683dca424 100644 --- a/TeslaSolarCharger/Server/Program.cs +++ b/TeslaSolarCharger/Server/Program.cs @@ -1,21 +1,14 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.OpenApi.Models; using Serilog; using Serilog.Context; -using System.Diagnostics; using System.Reflection; -using TeslaSolarCharger.Client.Pages; using TeslaSolarCharger.Model.Contracts; using TeslaSolarCharger.Server; using TeslaSolarCharger.Server.Contracts; using TeslaSolarCharger.Server.Resources.PossibleIssues.Contracts; using TeslaSolarCharger.Server.Scheduling; -using TeslaSolarCharger.Server.Services.ApiServices.Contracts; using TeslaSolarCharger.Server.Services.Contracts; -using TeslaSolarCharger.Server.Services.GridPrice.Contracts; using TeslaSolarCharger.Services; -using TeslaSolarCharger.Services.Services.Contracts; using TeslaSolarCharger.Shared; using TeslaSolarCharger.Shared.Contracts; using TeslaSolarCharger.Shared.Dtos.Contracts; diff --git a/TeslaSolarCharger/Server/Services/BackendApiService.cs b/TeslaSolarCharger/Server/Services/BackendApiService.cs index 6b34c71f2..b36bbe554 100644 --- a/TeslaSolarCharger/Server/Services/BackendApiService.cs +++ b/TeslaSolarCharger/Server/Services/BackendApiService.cs @@ -77,7 +77,7 @@ internal string GenerateAuthUrl(DtoTeslaOAuthRequestInformation oAuthInformation { logger.LogTrace("{method}({@oAuthInformation})", nameof(GenerateAuthUrl), oAuthInformation); var url = - $"https://auth.tesla.com/oauth2/v3/authorize?&client_id={Uri.EscapeDataString(oAuthInformation.ClientId)}&locale={Uri.EscapeDataString(locale)}&prompt={Uri.EscapeDataString(oAuthInformation.Prompt)}&redirect_uri={Uri.EscapeDataString(oAuthInformation.RedirectUri)}&response_type={Uri.EscapeDataString(oAuthInformation.ResponseType)}&scope={Uri.EscapeDataString(oAuthInformation.Scope)}&state={Uri.EscapeDataString(oAuthInformation.State)}"; + $"https://auth.tesla.com/oauth2/v3/authorize?&client_id={Uri.EscapeDataString(oAuthInformation.ClientId)}&locale={Uri.EscapeDataString(locale)}&prompt={Uri.EscapeDataString(oAuthInformation.Prompt)}&redirect_uri={Uri.EscapeDataString(oAuthInformation.RedirectUri)}&response_type={Uri.EscapeDataString(oAuthInformation.ResponseType)}&scope={Uri.EscapeDataString(oAuthInformation.Scope)}&state={Uri.EscapeDataString(oAuthInformation.State)}&prompt_missing_scopes=true"; return url; } diff --git a/TeslaSolarCharger/Server/Services/ErrorHandlingService.cs b/TeslaSolarCharger/Server/Services/ErrorHandlingService.cs index 94339490c..d3c95f995 100644 --- a/TeslaSolarCharger/Server/Services/ErrorHandlingService.cs +++ b/TeslaSolarCharger/Server/Services/ErrorHandlingService.cs @@ -425,7 +425,7 @@ await AddOrRemoveErrors(activeErrors, issueKeys.FleetApiTokenUnauthorized, "Flee "You recently changed your password or did not enable mobile access in your car. Enable mobile access in your car and open the Base Configuration and request a new token. Important: You need to allow access to all selectable scopes.", tokenState == FleetApiTokenState.TokenUnauthorized).ConfigureAwait(false); await AddOrRemoveErrors(activeErrors, issueKeys.FleetApiTokenMissingScopes, "Your Tesla token has missing scopes.", - "Remove Tesla Solar Charger from your third party apps as you won't get asked again for the scopes. After that request a new token in the Base Configuration and select all available scopes.", + "Open the Base Configuration and request a new token. Note: You need to allow all selectable scopes as otherwise TSC won't work properly.", tokenState == FleetApiTokenState.MissingScopes).ConfigureAwait(false); await AddOrRemoveErrors(activeErrors, issueKeys.FleetApiTokenRequestExpired, "Tesla Token could not be received", "Open the Base Configuration and request a new token.", @@ -479,7 +479,7 @@ private async Task AddOrRemoveErrors(List activeErrors, string issu } else if (shouldBeActive) { - for (var i = 0; i < activeErrors.Count; i++) + for (var i = 0; i < filteredErrors.Count; i++) { if (i == 0) { diff --git a/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs b/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs index 500f37bf1..d51761ae1 100644 --- a/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs +++ b/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs @@ -1169,18 +1169,13 @@ public async Task RefreshTokensIfAllowedAndNeeded() logger.LogError("No token found. Cannot refresh token."); return; } + var tokensToRefresh = tokens.Where(t => t.ExpiresAtUtc < (dateTimeProvider.UtcNow() + TimeSpan.FromMinutes(2))).ToList(); if (tokensToRefresh.Count < 1) { logger.LogTrace("No token needs to be refreshed."); return; } - //ToDo: needs to handle manual generated tokens. For now as soon as rate limits are introduced nobody gets refresh tokens even if they have a token not from www.teslasolarcharger.de - if (settings.AllowUnlimitedFleetApiRequests == false) - { - logger.LogError("Due to rate limitations fleet api requests are not allowed. As this version can not handle rate limits try updating to the latest version."); - return; - } foreach (var tokenToRefresh in tokensToRefresh) { @@ -1217,6 +1212,13 @@ await errorHandlingService.HandleError(nameof(TeslaFleetApiService), nameof(Send await HandleNonSuccessTeslaApiStatusCodes(response.StatusCode, tokenToRefresh, responseString, TeslaApiRequestType.Other).ConfigureAwait(false); } response.EnsureSuccessStatusCode(); + if (settings.AllowUnlimitedFleetApiRequests == false) + { + logger.LogError("Due to rate limitations fleet api requests are not allowed. As this version can not handle rate limits try updating to the latest version."); + teslaSolarChargerContext.TeslaTokens.Remove(tokenToRefresh); + await teslaSolarChargerContext.SaveChangesAsync().ConfigureAwait(false); + return; + } var newToken = JsonConvert.DeserializeObject(responseString) ?? throw new InvalidDataException("Could not get token from string."); tokenToRefresh.AccessToken = newToken.AccessToken; tokenToRefresh.RefreshToken = newToken.RefreshToken; @@ -1251,11 +1253,11 @@ public async Task RefreshFleetApiRequestsAreAllowed() } var responseValue = JsonConvert.DeserializeObject>(responseString); - settings.AllowUnlimitedFleetApiRequests = responseValue?.Value != false; + settings.AllowUnlimitedFleetApiRequests = responseValue?.Value == true; } catch (Exception) { - settings.AllowUnlimitedFleetApiRequests = true; + settings.AllowUnlimitedFleetApiRequests = false; } }