Created
December 29, 2020 22:30
-
-
Save eiannone/c70a65226693a3779b97ef06ff418884 to your computer and use it in GitHub Desktop.
Generatore di token per API Tesla
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {request} from 'https'; | |
function errore(msg) { | |
console.log(msg) | |
process.exit(-1); | |
} | |
if (process.argv.length < 4) { | |
errore("Richiamare lo script passando username e password tesla separati da spazio"); | |
} | |
const post_data = JSON.stringify({ | |
'grant_type': "password", | |
'email': process.argv[2], | |
'password': process.argv[3], | |
'client_id': "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384", | |
'client_secret': "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3" | |
}); | |
const req = request('https://owner-api.teslamotors.com/oauth/token', { | |
headers: { | |
'user-agent': "TeslaEma", | |
'Content-Type': 'application/json', | |
'Content-Length': post_data.length | |
}, | |
timeout: 30000, | |
method: 'POST' | |
}, res => { | |
if (res.statusCode > 199 && res.statusCode < 300) { | |
res.setEncoding('utf8'); | |
let rawData = ''; | |
res.on('data', chunk => { rawData += chunk; }); | |
res.on('end', () => { | |
try { | |
console.log(JSON.parse(rawData)); | |
} catch(err) { | |
errore("Errore: " + err); | |
} | |
}); | |
} else { | |
errore("Errore: " + res.statusMessage + " ("+res.statusCode+")"); | |
} | |
}); | |
req.on('error', e => errore("Errore: " + e.message + " ("+e.code+")")); | |
req.write(post_data); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment