Skip to content

Instantly share code, notes, and snippets.

@malkab
Created February 17, 2022 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malkab/0551a9b6316c2b85b78de6b9c8f80039 to your computer and use it in GitHub Desktop.
Save malkab/0551a9b6316c2b85b78de6b9c8f80039 to your computer and use it in GitHub Desktop.
cURL: Accessing a Bearer Token Auth Service with bash
#!/bin/bash
# ----------------
#
# Authorizes an user, retrieves the Bearer token and calls a securized
# API entry.
#
# ----------------
# Auth credentials
API_USER=the_user
API_PASS=the_pass
echo
# Gets the token. This retrieves a JSON like this
#
# { success: { accessToken: "XXXXX" } }
#
# that is parsed with jq to retrieve the token.
TOKEN=$(curl -k -X POST \
-d "user=${API_USER}&pass=${API_PASS}" \
https://whatever.com/auth/login | jq -r ".success.accessToken")
echo
curl -k -H "Authorization: Bearer ${TOKEN}" \
https://whatever.com/securized --output result.json
echo
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment