Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env bash
# Remember to install jq (e.g. brew install jq; yum install jq; apt-get install jq)
set -e
printf "${RED}Getting OAuth2 token from Keycloak (includes access_token, refresh_token, etc):${NORMAL}\n"
KEYCLOAK_RESPONSE=`curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'username=rincewind' -d 'password=apiman' -d 'grant_type=password' -d 'client_id=apiman'`
printf "$KEYCLOAK_RESPONSE \n\n"
printf "${RED}Parsing access_token field, as we don't need the other elements:${NORMAL}\n"
ACCESS_TOKEN=`echo "$KEYCLOAK_RESPONSE" | jq -r '.access_token'`
printf "$ACCESS_TOKEN \n\n"
printf "${RED}Accessing service via apiman using token - we will put it into the Authorization header:${NORMAL}\n"
curl -k -v -H "Authorization: Bearer $ACCESS_TOKEN"
printf "\n\n"
printf "${RED}You can also use the access_token query parameter instead if you wish. Let's try it:${NORMAL}\n"
curl -k -v"$ACCESS_TOKEN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.