diff --git a/providers/tesla/tesla-api.ts b/providers/tesla/tesla-api.ts index baca74ab..95f7a4fa 100644 --- a/providers/tesla/tesla-api.ts +++ b/providers/tesla/tesla-api.ts @@ -82,7 +82,7 @@ export class TeslaAPI { public async renewToken(refresh_token: string): Promise { try { - log(LogLevel.Trace, `renewToken(${refresh_token})`); + log(LogLevel.Trace, `TeslaAPI.renewToken(${refresh_token})`); // Tesla authAPI expects form data in the body const formData = new URLSearchParams(); @@ -91,10 +91,10 @@ export class TeslaAPI { formData.append("refresh_token", refresh_token); const authResponse = (await this.authAPI.post("/oauth2/v3/token", formData.toString())) as any; - log(LogLevel.Trace, `renewToken response: ${JSON.stringify(authResponse)}`); + log(LogLevel.Trace, `TeslaAPI.renewToken(${refresh_token}) response: ${JSON.stringify(authResponse)}`); return this.parseTokenResponse(authResponse); } catch (e) { - console.debug(`TeslaAPI.renewToken error: ${e}`); + console.debug(`TeslaAPI.renewToken(${refresh_token}) error: ${e}`); throw e; } } diff --git a/providers/tesla/tesla-server.ts b/providers/tesla/tesla-server.ts index 189c5d40..038bba36 100644 --- a/providers/tesla/tesla-server.ts +++ b/providers/tesla/tesla-server.ts @@ -46,7 +46,14 @@ export async function maintainToken( log(LogLevel.Trace, `Token ${token.access_token} is still valid`); return token as TeslaToken; } - log(LogLevel.Trace, `Token ${token.access_token} is invalid, calling renewToken`); + + assert(token.refresh_token !== undefined); + if (token.access_token) { + log(LogLevel.Trace, `Token ${token.access_token} is expired, calling renewToken`); + } else { + log(LogLevel.Trace, `Client pre-emptively requrested token refresh on ${token.refresh_token}`); + } + const newToken = await teslaAPI.renewToken(token.refresh_token); validToken(db, token.refresh_token, newToken); return newToken;