Fix tesla token not refreshed
This commit is contained in:
parent
eeb3082504
commit
48607e2e06
2 changed files with 4 additions and 10 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue