Advertisement
funcelot

get_token.sh

Feb 3rd, 2020
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.04 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. set -e
  3.  
  4. cd "${0%/*}"
  5.  
  6. LOCAL_ONLY="$(pwd)/local_only/local_only.sh"
  7.  
  8. if [ -f "$LOCAL_ONLY" ]; then
  9.     . $(pwd)/local_only/local_only.sh
  10. fi
  11.  
  12. cd $PROJECT_DIR
  13.  
  14. encode() {
  15.     local _length="${#1}"
  16.     for (( _offset = 0 ; _offset < _length ; _offset++ )); do
  17.         _print_offset="${1:_offset:1}"
  18.         case "${_print_offset}" in
  19.             [a-zA-Z0-9.~_-]) printf "${_print_offset}" ;;
  20.             ' ') printf + ;;
  21.             *) printf '%%%X' "'${_print_offset}" ;;
  22.         esac
  23.     done
  24. }
  25. CLIENT_ID=$(encode $CLIENT_ID)
  26. CLIENT_SECRET=$(encode $CLIENT_SECRET)
  27. RESOURCE_URI=$(encode "https://management.azure.com/")
  28. APP_TENTANT_ID=$(encode $APP_TENTANT_ID)
  29. FORMDATA="grant_type=client_credentials&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET"
  30. 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)
  31. echo $REQUEST_TOKEN | python3 -c "import sys, json; print(json.load(sys.stdin)['access_token'])"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement