Manually Invoking Web Services with Very Short-Lived Tokens

This blog post explains one way that you can use the curl command to invoke Webservice APIs where those services depend on very short-lived tokens that you must retrieve from another service.

I must pass an Authentication token to services:

curl -X GET <SERVICE_API> -H "Authorization: Bearer <TOKEN>" | python3 -m json.tool | batcat -l json

To get the token, I have to pass a username and password to a different service (the application provides API keys, but I haven’t figured out how to use them):

curl -X 'POST' <AUTH_API> -d '{"email": "<EMAIL>”, "password": "<PASSWORD>" }' -H 'Content-Type: application/json'

By the time I copy the value returned from the authentication service to the command line for the API, it has already expired.

I can use back ticks (`) to pass the value from the first curl command as a parameter to the second. The data looks like this:

{
    "accessToken": "<value>",
    "refreshToken": "<value>"
}

 I can use the jq command to parse this.

sudo apt install jq

Finally:

curl -X GET <SERVICE_API> -H "Authorization: Bearer `curl -X 'POST' <AUTH_API> -d '{"email": "<EMAIL>", "password": "<PASSWORD>" }' -H 'Content-Type: application/json' | jq -r '.accessToken'`" | python3 -m json.tool | batcat

Leave a comment