Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refreshing #1

Closed
IMgoRt opened this issue Feb 7, 2021 · 3 comments
Closed

Refreshing #1

IMgoRt opened this issue Feb 7, 2021 · 3 comments

Comments

@IMgoRt
Copy link

IMgoRt commented Feb 7, 2021

Hi,
I have a valid refresh token, how would I subsequently execute your code using the refresh_token rather than starting from email/password?

Thanks

@fredli74
Copy link
Owner

fredli74 commented Feb 8, 2021

There's no built-in function for refreshing a token in the HTML page. But if you run it locally with node.js it's super easy making your own, just using the cleaned-up teslaAuth.js library I added in the latest commit.

Clone or download the repository. Then create a refresh.js file in the root (next to the teslaAuth.js file), just containing something like this.

const teslaAuth = require("./teslaAuth");

const token = teslaAuth
    .refreshToken("<<<YOUR REFRESH TOKEN>>>")
    .then(t => console.log(t))

Run it with node refresh.js

The refresh call is a simple POST to the Tesla server. You can just curl it if you like.

curl --header "Content-Type: application/json" --data '{
 "grant_type":"refresh_token",
 "scope":"openid email offline_access",
 "client_id":"ownerapi",
 "refresh_token":"<<<YOUR REFRESH TOKEN>>>"
}' https://auth.tesla.com/oauth2/v3/token

@fredli74
Copy link
Owner

fredli74 commented Feb 8, 2021

The teslaAuth.js file is all you need from my repository if you want to put the function in your own project. Just copy that single file and then you can use the authenticate, refreshToken, and ownerapiToken functions. They're all documented inside the source.

/**
* Authenticate with Tesla OAuth 2.0 authentication server (v3)
*
* @export
* @throws On any encountered error
* @param {string} identity - Tesla account email address
* @param {string} credential - Tesla account password
* @param {string|undefined} passcode - Multi-Factor Authentication passcode (optional)
* @returns {Object} Returns the Tesla server token response
*/
async function authenticate(identity, credential, passcode) {

/**
* Obtain a new authentication token from a refresh_token
*
* @param {string} refresh_token - refresh_token previously collected from the Tesla authentication API
* @returns {Object} Returns the Tesla server token response
*/
async function refreshToken(refresh_token) {

/**
* Exchange an authentication access_token for a owner-api access_token
*
* @param {string} bearerToken - access_token part of the authentication token (v3)
* @returns {Object} Returns the Tesla server token response
*/
async function ownerapiToken(bearerToken) {

@IMgoRt
Copy link
Author

IMgoRt commented Feb 8, 2021

The teslaAuth.js file is all you need from my repository if you want to put the function in your own project. Just copy that single file and then you can use the authenticate, refreshToken, and ownerapiToken functions. They're all documented inside the source.

/**
* Authenticate with Tesla OAuth 2.0 authentication server (v3)
*
* @export
* @throws On any encountered error
* @param {string} identity - Tesla account email address
* @param {string} credential - Tesla account password
* @param {string|undefined} passcode - Multi-Factor Authentication passcode (optional)
* @returns {Object} Returns the Tesla server token response
*/
async function authenticate(identity, credential, passcode) {

/**
* Obtain a new authentication token from a refresh_token
*
* @param {string} refresh_token - refresh_token previously collected from the Tesla authentication API
* @returns {Object} Returns the Tesla server token response
*/
async function refreshToken(refresh_token) {

/**
* Exchange an authentication access_token for a owner-api access_token
*
* @param {string} bearerToken - access_token part of the authentication token (v3)
* @returns {Object} Returns the Tesla server token response
*/
async function ownerapiToken(bearerToken) {

That's great, thanks for taking the time to reply, I'll try it out in the morning (01:11 here)

@fredli74 fredli74 closed this as completed Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants