@eiannone/tesla-api 1.14.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.
- package/TeslaApi.js +9 -56
- package/TeslaStream.js +1 -1
- 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
|
|
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
|
|
|
177
|
+
// https://tesla-api.timdorr.com/api-basics/authentication
|
|
219
178
|
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
|
-
};
|
|
226
179
|
try {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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);
|
package/TeslaStream.js
CHANGED
|
@@ -58,7 +58,7 @@ export default class TeslaStream extends EventEmitter {
|
|
|
58
58
|
}
|
|
59
59
|
if (this.ws.readyState == WebSocket.CONNECTING) {
|
|
60
60
|
this.ws.removeAllListeners('open');
|
|
61
|
-
this.ws.on('open', _ =>
|
|
61
|
+
this.ws.on('open', _ => this.disconnect(reconnect, unsubscribe));
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
64
|
this.state = CLOSING;
|