From 48607e2e06817b1fb62a033c7a21d31a98662fac Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 12 Feb 2021 12:00:43 +0100 Subject: [PATCH] Fix tesla token not refreshed --- vehicle/tesla.go | 6 +++--- vehicle/tesla/client.go | 8 +------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/vehicle/tesla.go b/vehicle/tesla.go index 2467a8983..67e143893 100644 --- a/vehicle/tesla.go +++ b/vehicle/tesla.go @@ -71,9 +71,8 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) { } // authenticated http client with logging - ctx := context.Background() + ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewHelper(log).Client) http := authClient.Config.Client(ctx, token) - http.Transport = request.NewTripper(log, http.Transport) // injected to the Tesla client client := &tesla.Client{HTTP: http} @@ -104,7 +103,7 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) { return v, nil } -// token creates the Tesla access token +// teslaToken creates the Tesla OAuth token from given credentials func teslaToken(auth *auth.Client, user, password string, tokens teslaTokens) (*oauth2.Token, error) { // without tokens try to login - will fail if MFA enabled if tokens.Access == "" { @@ -121,6 +120,7 @@ func teslaToken(auth *auth.Client, user, password string, tokens teslaTokens) (* ts := auth.Config.TokenSource(ctx, &oauth2.Token{ AccessToken: tokens.Access, RefreshToken: tokens.Refresh, + Expiry: time.Now(), }) // test the token source diff --git a/vehicle/tesla/client.go b/vehicle/tesla/client.go index 7345148cd..8fe0f71e0 100644 --- a/vehicle/tesla/client.go +++ b/vehicle/tesla/client.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "net/http" "github.com/andig/evcc/util" "github.com/andig/evcc/util/request" @@ -46,11 +45,6 @@ func pkce() (verifier, challenge string, err error) { // NewClient creates a tesla authentication client func NewClient(log *util.Logger) (*Client, error) { - httpClient := &http.Client{ - Timeout: request.Timeout, - Transport: request.NewTripper(log, http.DefaultTransport), - } - config := &oauth2.Config{ ClientID: "ownerapi", ClientSecret: "", @@ -68,7 +62,7 @@ func NewClient(log *util.Logger) (*Client, error) { } auth := &tesla.Auth{ - Client: httpClient, + Client: request.NewHelper(log).Client, AuthURL: config.AuthCodeURL(state(), oauth2.AccessTypeOffline, oauth2.SetAuthURLParam("code_challenge", challenge), oauth2.SetAuthURLParam("code_challenge_method", "S256"),