Files
OptionRecorderSchwab/getNewAccessToken.sh

30 lines
1.3 KiB
Bash
Raw Normal View History

2024-05-09 15:29:45 -05:00
#!/bin/bash
#set -x
mkdir -p temp
2024-09-03 10:32:56 -05:00
ageInSec=$(($(date +%s) - $(date +%s -r "access_token.dat")))
2024-09-03 10:32:56 -05:00
#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
2024-05-09 15:29:45 -05:00
fi
2024-09-03 10:32:56 -05:00
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...
## after all they send it so we should probably process it.
#jq -e -r .refresh_token temp/accessTokenReqResp.$$.json > temp/refresh_token.dat
#if [[ $? -eq 0 ]]; then
# mv temp/refresh_token.dat refresh_token.dat
#fi
##echo Refresh Token: $(<refresh_token.dat)
#