Skip to content

Instantly share code, notes, and snippets.

@msavy
Created January 8, 2016 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save msavy/eaa1841e0c7a50e6ea8c to your computer and use it in GitHub Desktop.
Save msavy/eaa1841e0c7a50e6ea8c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Remember to install jq (e.g. brew install jq; yum install jq; apt-get install jq)
set -e
RED='\033[0;31m'
NORMAL='\033[0m'
printf "${RED}Getting OAuth2 token from Keycloak (includes access_token, refresh_token, etc):${NORMAL}\n"
KEYCLOAK_RESPONSE=`curl -s -X POST http://127.0.0.1:8080/auth/realms/stottie/protocol/openid-connect/token -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" https://127.0.0.1:8443/apiman-gateway/Newcastle/EchoAPI/1.0
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 https://127.0.0.1:8443/apiman-gateway/Newcastle/EchoAPI/1.0?access_token="$ACCESS_TOKEN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment