@eiannone/tesla-api 1.10.0 → 1.11.0
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 +60 -13
- package/examples/refresh-token.js +11 -0
- package/package.json +1 -1
package/TeslaApi.js
CHANGED
|
@@ -129,10 +129,51 @@ class TeslaApi {
|
|
|
129
129
|
return this.#apiCall(vid + "/command/" + command, "POST", params);
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
async #oauthCall(params) {
|
|
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
|
+
// 'Content-Type': 'application/json',
|
|
139
|
+
// 'Content-Length': postData.length
|
|
140
|
+
// },
|
|
141
|
+
// timeout: 30000,
|
|
142
|
+
// method: 'POST'
|
|
143
|
+
// }, res => {
|
|
144
|
+
// if (res.statusCode > 199 && res.statusCode < 300) {
|
|
145
|
+
// res.setEncoding('utf8');
|
|
146
|
+
// let rawData = '';
|
|
147
|
+
// res.on('data', chunk => { rawData += chunk; });
|
|
148
|
+
// res.on('end', () => {
|
|
149
|
+
// try {
|
|
150
|
+
// resolve(JSON.parse(rawData));
|
|
151
|
+
// } catch(err) {
|
|
152
|
+
// reject(new ApiError(err));
|
|
153
|
+
// }
|
|
154
|
+
// });
|
|
155
|
+
// } else {
|
|
156
|
+
// let errMsg = res.statusMessage + " ("+res.statusCode+")";
|
|
157
|
+
// reject(new ApiError(errMsg, this.#decodeStatus(res.statusCode)));
|
|
158
|
+
// }
|
|
159
|
+
// });
|
|
160
|
+
// req.on('error', e => {
|
|
161
|
+
// // Error code examples:
|
|
162
|
+
// // - EAI_AGAIN (DNS lookup timeout)
|
|
163
|
+
// // - ECONNRESET
|
|
164
|
+
// // - ECONNREFUSED
|
|
165
|
+
// // - ENOTFOUND
|
|
166
|
+
// reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
|
|
167
|
+
// });
|
|
168
|
+
// req.write(postData);
|
|
169
|
+
// req.end();
|
|
170
|
+
// });
|
|
171
|
+
// }
|
|
172
|
+
|
|
173
|
+
async #oauthCall2(params) {
|
|
133
174
|
return new Promise((resolve, reject) => {
|
|
134
175
|
const postData = JSON.stringify(params);
|
|
135
|
-
const req = request(
|
|
176
|
+
const req = request('https://auth.tesla.com/oauth2/v3/token', {
|
|
136
177
|
headers: {
|
|
137
178
|
'user-agent': "TeslaEma",
|
|
138
179
|
'Content-Type': 'application/json',
|
|
@@ -167,7 +208,7 @@ class TeslaApi {
|
|
|
167
208
|
});
|
|
168
209
|
req.write(postData);
|
|
169
210
|
req.end();
|
|
170
|
-
});
|
|
211
|
+
});
|
|
171
212
|
}
|
|
172
213
|
|
|
173
214
|
onTokenRefreh(callback) {
|
|
@@ -175,7 +216,13 @@ class TeslaApi {
|
|
|
175
216
|
}
|
|
176
217
|
|
|
177
218
|
async refreshToken(refresh_token) {
|
|
178
|
-
|
|
219
|
+
const payLoad = {
|
|
220
|
+
grant_type: 'refresh_token',
|
|
221
|
+
client_id: 'ownerapi',
|
|
222
|
+
refresh_token,
|
|
223
|
+
scope: 'openid email offline_access'
|
|
224
|
+
};
|
|
225
|
+
return this.#oauthCall2(payLoad)
|
|
179
226
|
.then(async (resp) => {
|
|
180
227
|
this.token = resp.access_token;
|
|
181
228
|
this.refresh_token = resp.refresh_token;
|
|
@@ -190,15 +237,15 @@ class TeslaApi {
|
|
|
190
237
|
});
|
|
191
238
|
}
|
|
192
239
|
|
|
193
|
-
async getTokens(email, password) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
240
|
+
// async getTokens(email, password) {
|
|
241
|
+
// return this.#oauthCall({
|
|
242
|
+
// 'grant_type': "password",
|
|
243
|
+
// 'email': email,
|
|
244
|
+
// 'password': password,
|
|
245
|
+
// 'client_id': "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384",
|
|
246
|
+
// 'client_secret': "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3"
|
|
247
|
+
// });
|
|
248
|
+
// }
|
|
202
249
|
}
|
|
203
250
|
|
|
204
251
|
export { ApiError, TeslaApi }
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {TeslaApi, ApiError} from '../TeslaApi.js';
|
|
2
|
+
|
|
3
|
+
const api = new TeslaApi();
|
|
4
|
+
api.refreshToken('1234')
|
|
5
|
+
.then(result => console.log(JSON.stringify(result)))
|
|
6
|
+
.catch(err => {
|
|
7
|
+
let reason = (err instanceof ApiError)? err.reason : ApiError.UNKNOWN;
|
|
8
|
+
const errMsg = (err instanceof Error)? err.message : err;
|
|
9
|
+
console.error("ApiError " + reason + ": " + errMsg);
|
|
10
|
+
process.exit(-1);
|
|
11
|
+
});
|