-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.js
66 lines (53 loc) · 1.86 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "_" }] */
/* global FirestoreRequest_, Utilities, base64EncodeSafe_ */
/**
* See https://tesla-api.timdorr.com/api-basics/authentication
* @private
* @returns {string} the access token needed for making future requests
*/
function getAuthToken_ (email, password) {
var access_token = PropertiesService.getScriptProperties().getProperty("access_token")
if (!access_token) {
var access_token = getoAuthTokens_(email, password).access_token;
}
return access_token
}
function updateClientID_() {
//refreshing ID from pastebin:
//https://tesla-api.timdorr.com/api-basics/authentication
const PS = PropertiesService.getScriptProperties();
var keys =
UrlFetchApp.fetch("https://pastebin.com/raw/pS7Z6yyP").getContentText().split('\r\n').forEach(function(c) {
var part = c.split("=");
PS.setProperty([part[0]], part[1]);
})
}
function getoAuthTokens_(email, password) {
const PS = PropertiesService.getScriptProperties();
var payload = {
"grant_type": "password",
"client_id": PS.getProperty("TESLA_CLIENT_ID"),
"client_secret": PS.getProperty("TESLA_CLIENT_SECRET"),
"email": email,
"password": password
}
var t = new TeslaRequest_("/oauth/token?grant_type=password", null, {payload: payload}).post()
if (t.access_token) {
PS.setProperties(t);
}
return t;
}
function refreshToken_() {
const PS = PropertiesService.getScriptProperties();
var payload = {
"grant_type": "password",
"client_id": PS.getProperty("TESLA_CLIENT_ID"),
"client_secret": PS.getProperty("TESLA_CLIENT_SECRET"),
"refresh_token": PS.getProperty("refresh_token")
}
var t = new TeslaRequest_("/oauth/token?grant_type=refresh_token", null, {payload: payload}).post()
if (t.access_token) {
PS.setProperties(t);
}
return t;
}