change the access token to be cached

This commit is contained in:
2024-09-03 10:32:56 -05:00
parent 1b94d1d8dc
commit bd682f670f
15 changed files with 29 additions and 40 deletions

View File

@@ -2,17 +2,21 @@
#set -x
mkdir -p temp
ageInSec=$(($(date +%s) - $(date +%s -r "access_token.dat")))
curl -s -X POST https://api.schwabapi.com/v1/oauth/token \
-H "Authorization: Basic $(<appCredentials.dat)" \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d "grant_type=refresh_token&refresh_token=$(<refresh_token.dat)" > temp/accessTokenReqResp.$$.json
jq -e -r .access_token temp/accessTokenReqResp.$$.json > access_token.dat
if [[ $? -ne 0 ]]; then
curl -d "" -X POST "https://deadswitch.kawomi.com/api/v1?topic=trade.schwab.error.getNewAccessToken&secs=1&email=joe@kawomi.com"
throw "ERROR: GETTING THE ACCESS TOKEN FAILED! $(<temp/accessTokenReqResp.$$.json)" err
#Cache the last token for 10 min.
if [[ ${ageInSec} -gt 600 ]]; then
curl -s -X POST https://api.schwabapi.com/v1/oauth/token \
-H "Authorization: Basic $(<appCredentials.dat)" \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d "grant_type=refresh_token&refresh_token=$(<refresh_token.dat)" > temp/accessTokenReqResp.$$.json
jq -e -r .access_token temp/accessTokenReqResp.$$.json > access_token.dat
if [[ $? -ne 0 ]]; then
curl -d "" -X POST "https://deadswitch.kawomi.com/api/v1?topic=trade.schwab.error.getNewAccessToken&secs=1&email=joe@kawomi.com"
throw "ERROR: GETTING THE ACCESS TOKEN FAILED! $(<temp/accessTokenReqResp.$$.json)" err
fi
fi
#echo Access Token: $(<access_token.dat)
echo -n -e "$(<access_token.dat)"
## I found the refresh token to be empty after the network is unreachable, therefore I don't handle the refresh token here...
## I have not seen it change, but let's see if they give us a new one before expiry...