Skip to main content

Hey All,

Running into an issue, I assume it’s simple, but I’m blanking as to what I am doing wrong.

 

#!/bin/zsh --no-rcs

client_id="REMOVED"
client_secret="REMOVED"
url="https://REMOVED"

##Gets Access Token
getAccessToken() {
response=$(curl --silent --location --request POST "${url}/api/oauth/token" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "client_id=${client_id}" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_secret=${client_secret}")
access_token=$(echo "$response" | plutil -extract access_token raw -)
token_expires_in=$(echo "$response" | plutil -extract expires_in raw -)
token_expiration_epoch=$(($current_epoch + $token_expires_in - 1))
}

checkTokenExpiration() {
current_epoch=$(date +%s)
if > token_expiration_epoch -ge current_epoch ]]
then
echo "Token valid until the following epoch time: " "$token_expiration_epoch"
else
echo "No valid token available, getting new token"
getAccessToken
fi
}

# I run the function here, and it provides me the access token and timer of '59'
getAccessToken
echo $access_token
echo $token_expires_in
echo $token_expiration_epoch


# I then run the function to check on expiration, and it tells me no valid token
checkTokenExpiration

# I recreates the token gives me 59 again.
echo $token_expires_in

Results:

access token info removed (But it does show the full access token

59

58

No valid token available, getting new token

59

 

Anyone able to tell what I’m missing?

For more clarification, the client ID and secret ID authenticate properly to the Jamp API site.

I receive a 401 error when actually trying to GET a request.

I’ve confirmed the permissions are correct for the API client. Even if my checkTokenExpiration function isn’t right, I must be missing something else.


Well, I figured it out.

A couple things I was doing wrong. I had a variable in the function that was never set for current_epoch which is why it keep showing it was invalid when it wasn’t.

The other failure I was having is when I actually would try to use a GET request that was providing the 401 error, it’s because I was using single quotes rather than double. That caused expansion problems.