Skip to content

Instantly share code, notes, and snippets.

@beikeland
Created December 25, 2019 00:19
Show Gist options
  • Save beikeland/397fecb395092459032bbca0dfe39198 to your computer and use it in GitHub Desktop.
Save beikeland/397fecb395092459032bbca0dfe39198 to your computer and use it in GitHub Desktop.
Fetches Tesla OAUTH token to use with API from shell. Also spits out refresh token and expiry information.
#!/bin/bash
echo -n "USERNAME: "; read uname
echo -n "PASSWORD: "; stty -echo; read passwd; stty echo; echo
OAUTH=$(curl -s -i -X POST -H 'Content-Type: application/json' -d "{\"grant_type\": \"password\", \"client_id\": \"81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384\", \"client_secret\": \"c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3\", \"email\": \"$uname\",\"password\": \"$passwd\"}" "https://owner-api.teslamotors.com/oauth/token")
if [[ "$OAUTH" =~ \"access_token\":\ ?\"([0-9a-f]+)\" ]]; then
access_token=${BASH_REMATCH[1]}
else
echo "Error, no access token received:"
echo $OAUTH
exit 1
fi
if [[ "$OAUTH" =~ \"created_at\":\ ?([0-9]+) ]]; then
created_at=${BASH_REMATCH[1]}
fi
if [[ "$OAUTH" =~ \"expires_in\":\ ?([0-9]+) ]]; then
expires_in=${BASH_REMATCH[1]}
fi
if [[ "$OAUTH" =~ \"refresh_token\":\ ?\"([0-9a-f]+)\" ]]; then
refresh_token=${BASH_REMATCH[1]}
fi
echo "Success!"
echo "access_token $access_token"
echo "expires_in $expires_in"
echo "refresh_token $refresh_token"
echo "created_at $created_at"
expires_at=$((created_at+expires_in))
valid_from=`date -d "@$created_at" +'%Y-%m-%d %H:%M'`
valid_to=`date -d "@$expires_at" +'%Y-%m-%d %H:%M'`
echo "valid from $valid_from"
echo "valid to $valid_to"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment