Fix tesla token not refreshed
Some checks failed
Build / Build (push) Failing after 2m58s
Build / Publish :latest (push) Has been skipped
Build / Publish :release (push) Has been skipped
Build / Release (push) Has been skipped

This commit is contained in:
andig 2021-02-12 12:00:43 +01:00
parent eeb3082504
commit 48607e2e06
2 changed files with 4 additions and 10 deletions

View file

@ -71,9 +71,8 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
} }
// authenticated http client with logging // 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 := authClient.Config.Client(ctx, token)
http.Transport = request.NewTripper(log, http.Transport)
// injected to the Tesla client // injected to the Tesla client
client := &tesla.Client{HTTP: http} client := &tesla.Client{HTTP: http}
@ -104,7 +103,7 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return v, nil 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) { func teslaToken(auth *auth.Client, user, password string, tokens teslaTokens) (*oauth2.Token, error) {
// without tokens try to login - will fail if MFA enabled // without tokens try to login - will fail if MFA enabled
if tokens.Access == "" { if tokens.Access == "" {
@ -121,6 +120,7 @@ func teslaToken(auth *auth.Client, user, password string, tokens teslaTokens) (*
ts := auth.Config.TokenSource(ctx, &oauth2.Token{ ts := auth.Config.TokenSource(ctx, &oauth2.Token{
AccessToken: tokens.Access, AccessToken: tokens.Access,
RefreshToken: tokens.Refresh, RefreshToken: tokens.Refresh,
Expiry: time.Now(),
}) })
// test the token source // test the token source

View file

@ -8,7 +8,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"net/http"
"github.com/andig/evcc/util" "github.com/andig/evcc/util"
"github.com/andig/evcc/util/request" "github.com/andig/evcc/util/request"
@ -46,11 +45,6 @@ func pkce() (verifier, challenge string, err error) {
// NewClient creates a tesla authentication client // NewClient creates a tesla authentication client
func NewClient(log *util.Logger) (*Client, error) { func NewClient(log *util.Logger) (*Client, error) {
httpClient := &http.Client{
Timeout: request.Timeout,
Transport: request.NewTripper(log, http.DefaultTransport),
}
config := &oauth2.Config{ config := &oauth2.Config{
ClientID: "ownerapi", ClientID: "ownerapi",
ClientSecret: "", ClientSecret: "",
@ -68,7 +62,7 @@ func NewClient(log *util.Logger) (*Client, error) {
} }
auth := &tesla.Auth{ auth := &tesla.Auth{
Client: httpClient, Client: request.NewHelper(log).Client,
AuthURL: config.AuthCodeURL(state(), oauth2.AccessTypeOffline, AuthURL: config.AuthCodeURL(state(), oauth2.AccessTypeOffline,
oauth2.SetAuthURLParam("code_challenge", challenge), oauth2.SetAuthURLParam("code_challenge", challenge),
oauth2.SetAuthURLParam("code_challenge_method", "S256"), oauth2.SetAuthURLParam("code_challenge_method", "S256"),