I have a token whos expire in 30 minutes, i want do renew this token until the 30 minutes time expire, i have the following script calling an api:
function token {
gen_bearer_token=$(curl -X POST "https://api.foo.bar/oauth2/token" -H "accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" -d "client_id=foo&client_secret=bar" | cut -d '{' -f2 | cut -d '}' -f1)
bearer_token=$(echo $gen_bearer_token | awk '{print $2}' | cut -d '"' -f2 | cut -d '"' -f1)
token_type=$(echo $gen_bearer_token | awk '{print $6}' | cut -d '"' -f2 | cut -d '"' -f1)
}
echo -e "HOSTNAME;LAST_SEEN" > ${file}
ids=$(curl -s -X GET 'https://api.foo.bar/devices/queries/devices-scroll/v1?limit=5000' -H 'accept: application/json' -H 'authorization: '${token_type}' '${bearer_token}'' | jq .resources[])
for id in ${ids}
do
result=$(curl -s -X GET 'https://api.foo.bar/devices/entities/devices/v1?ids='${id}'' -H 'accept: application/json' -H 'authorization: '${token_type}' '${bearer_token}'' | jq '.resources[] | "\(.hostname) \(.last_seen)"')
if [[ ! -z ${result} ]]
then
hostname=$(echo ${result} | awk '{print $1}' | cut -d '"' -f2)
last_seen=$(echo ${result} | awk '{print $2}' | cut -d '"' -f1)
echo -e "${hostname};${last_seen}" >> ${file}
fi
done
in this looping, time duration is more than 2 hours (variable duration) + i want to create a timer to renew token if the times pass through 30 minutes, if passed 30 minutes the request in api will fail.
for id in ${ids}
do
result=$(curl -s -X GET 'https://api.foo.bar/devices/entities/devices/v1?ids='${id}'' -H 'accept: application/json' -H 'authorization: '${token_type}' '${bearer_token}'' | jq '.resources[] | "\(.hostname) \(.last_seen)"')
if [[ ! -z ${result} ]]
then
hostname=$(echo ${result} | awk '{print $1}' | cut -d '"' -f2)
last_seen=$(echo ${result} | awk '{print $2}' | cut -d '"' -f1)
echo -e "${hostname};${last_seen}" >> ${file}
fi
done