@eiannone/tesla-api 1.14.1 → 1.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/TeslaApi.js +15 -58
  2. package/package.json +1 -1
package/TeslaApi.js CHANGED
@@ -79,7 +79,7 @@ class TeslaApi {
79
79
  if (res.statusCode == 401 && this.refresh_token != null) {
80
80
  // Tries to refresh the tokens
81
81
  this.refreshToken(this.refresh_token)
82
- .then(_ => this.#apiCall(path, method))
82
+ .then(_ => this.#apiCall(path, method, params))
83
83
  .then(response => resolve(response))
84
84
  .catch(error => { reject(error); });
85
85
  return;
@@ -129,49 +129,7 @@ class TeslaApi {
129
129
  return this.#apiCall(vid + "/command/" + command, "POST", params);
130
130
  }
131
131
 
132
- async #oauthCall(params, bearer_token) {
133
- return new Promise((resolve, reject) => {
134
- const postData = JSON.stringify(params);
135
- const req = request(BASE_URL + '/oauth/token', {
136
- headers: {
137
- 'user-agent': "TeslaEma",
138
- 'Authorization': "Bearer " + bearer_token,
139
- 'Content-Type': 'application/json',
140
- 'Content-Length': postData.length
141
- },
142
- timeout: 30000,
143
- method: 'POST'
144
- }, res => {
145
- if (res.statusCode > 199 && res.statusCode < 300) {
146
- res.setEncoding('utf8');
147
- let rawData = '';
148
- res.on('data', chunk => { rawData += chunk; });
149
- res.on('end', () => {
150
- try {
151
- resolve(JSON.parse(rawData));
152
- } catch(err) {
153
- reject(new ApiError(err));
154
- }
155
- });
156
- } else {
157
- let errMsg = res.statusMessage + " ("+res.statusCode+")";
158
- reject(new ApiError(errMsg, this.#decodeStatus(res.statusCode)));
159
- }
160
- });
161
- req.on('error', e => {
162
- // Error code examples:
163
- // - EAI_AGAIN (DNS lookup timeout)
164
- // - ECONNRESET
165
- // - ECONNREFUSED
166
- // - ENOTFOUND
167
- reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
168
- });
169
- req.write(postData);
170
- req.end();
171
- });
172
- }
173
-
174
- async #oauthCall2(params) {
132
+ async #oauthCall(params) {
175
133
  return new Promise((resolve, reject) => {
176
134
  const postData = JSON.stringify(params);
177
135
  const req = request('https://auth.tesla.com/oauth2/v3/token', {
@@ -216,21 +174,16 @@ class TeslaApi {
216
174
  this.cb_refreshToken = callback;
217
175
  }
218
176
 
219
- async refreshToken(refresh_token) {
220
- const payLoad = {
221
- grant_type: 'refresh_token',
222
- client_id: 'ownerapi',
223
- refresh_token,
224
- scope: 'openid email offline_access'
225
- };
177
+ // https://tesla-api.timdorr.com/api-basics/authentication
178
+ async refreshToken(refresh_token, retry = 1) {
226
179
  try {
227
- let resp = await this.#oauthCall2(payLoad);
228
- this.refresh_token = resp.refresh_token;
229
- let oauth = await this.#oauthCall({
230
- grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
231
- client_id: '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384',
232
- client_secret: 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3'
233
- }, resp.access_token);
180
+ const oauth = await this.#oauthCall({
181
+ grant_type: 'refresh_token',
182
+ client_id: 'ownerapi',
183
+ refresh_token,
184
+ scope: 'openid email offline_access'
185
+ });
186
+ this.refresh_token = oauth.refresh_token;
234
187
  this.token = oauth.access_token;
235
188
  if (typeof this.cb_refreshToken == 'function') {
236
189
  this.cb_refreshToken(this.token, this.refresh_token);
@@ -238,6 +191,10 @@ class TeslaApi {
238
191
  return oauth;
239
192
  }
240
193
  catch(error) {
194
+ if (retry < 3) {
195
+ await new Promise(resolve => setTimeout(resolve, 500));
196
+ return this.refreshToken(refresh_token. retry + 1);
197
+ }
241
198
  if (error instanceof Error) error.message += " - Unable to refresh Token";
242
199
  throw error;
243
200
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eiannone/tesla-api",
3
- "version": "1.14.1",
3
+ "version": "1.15.2",
4
4
  "description": "Nodejs Tesla API",
5
5
  "type": "module",
6
6
  "main": "index.js",