Advertisement
funcelot

get_access_token.sh

Mar 13th, 2020
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.93 KB | None | 0 0
  1. #!/bin/bash -e
  2. encode() {
  3.     local _length="${#1}"
  4.     for (( _offset = 0 ; _offset < _length ; _offset++ )); do
  5.         _print_offset="${1:_offset:1}"
  6.         case "${_print_offset}" in
  7.             [a-zA-Z0-9.~_-]) printf "${_print_offset}" ;;
  8.             ' ') printf + ;;
  9.             *) printf '%%%X' "'${_print_offset}" ;;
  10.         esac
  11.     done
  12. }
  13. CLIENT_ID=$(encode $CLIENT_ID)
  14. CLIENT_SECRET=$(encode $CLIENT_SECRET)
  15. RESOURCE_URI=$(encode "https://management.azure.com/")
  16. APP_TENTANT_ID=$(encode $APP_TENTANT_ID)
  17. FORMDATA="grant_type=client_credentials&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET" #&resource=$RESOURCE_URI"
  18. REQUEST_TOKEN=$(curl -s -X POST "https://login.microsoftonline.com/$APP_TENTANT_ID/oauth2/token" -H "Content-Type: application/x-www-form-urlencoded" -d "$FORMDATA" 2>&1)
  19. ACCESS_TOKEN=$(echo "import json; token=json.loads('$REQUEST_TOKEN'); print(token[\"access_token\"])")
  20. python -c "$ACCESS_TOKEN"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement