@eiannone/tesla-api 1.15.0 → 1.15.1

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 +9 -58
  2. package/package.json +1 -1
package/TeslaApi.js CHANGED
@@ -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', {
@@ -218,22 +176,15 @@ class TeslaApi {
218
176
 
219
177
  // https://tesla-api.timdorr.com/api-basics/authentication
220
178
  async refreshToken(refresh_token) {
221
- const payLoad = {
222
- grant_type: 'refresh_token',
223
- client_id: 'ownerapi',
224
- refresh_token,
225
- scope: 'openid email offline_access'
226
- };
227
179
  try {
228
- let resp = await this.#oauthCall2(payLoad);
229
- this.refresh_token = resp.refresh_token;
230
- // let oauth = await this.#oauthCall({
231
- // grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
232
- // client_id: '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384',
233
- // client_secret: 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3'
234
- // }, resp.access_token);
235
- // this.token = oauth.access_token;
236
- this.token = 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;
187
+ this.token = oauth.access_token;
237
188
  if (typeof this.cb_refreshToken == 'function') {
238
189
  this.cb_refreshToken(this.token, this.refresh_token);
239
190
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eiannone/tesla-api",
3
- "version": "1.15.0",
3
+ "version": "1.15.1",
4
4
  "description": "Nodejs Tesla API",
5
5
  "type": "module",
6
6
  "main": "index.js",