Last active
November 24, 2021 07:38
-
-
Save htammen/d27fce518bf956f2e6cdc45f4a64fa82 to your computer and use it in GitHub Desktop.
Retrieve an access token for a btp cf app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Get BTP OAuth access token | |
# Usage: | |
# btp_accesstoken <appname> | |
# | |
# Call this bash script with btp_accesstoken <appname> where appname is a name | |
# of your apps on btp. | |
# You can retrieve the list of apps with 'cf a' | |
# Login credentials are retrieved from bitwarden. You have to be logged into it already | |
# client_id, client_secret, oauth endpoint are retrieved from 'cf de <appname>'. You have to be looged into it as well. | |
appname=$1 | |
function showHelp { | |
echo "usage: btp_accesstoken <appname>" | |
echo "You can retrieve the list of deployed apps with 'cf a'" | |
exit | |
} | |
if [ -z "$appname" ] | |
then | |
showHelp | |
fi | |
cf de ${appname} | |
# jq returns a string with leading and trailing quotes cut ... removes them | |
btp_oauth_url=$(cat default-env.json | jq '.VCAP_SERVICES.xsuaa[0].credentials.url' | cut -d '"' -f 2) | |
# the oauth endpoint | |
oauth_path="/oauth/token" | |
btp_oauth_url=${btp_oauth_url}${oauth_path} | |
client_id=$(cat default-env.json | jq .VCAP_SERVICES.xsuaa[0].credentials.clientid | cut -d'"' -f 2) | |
client_secret=$(cat default-env.json | jq .VCAP_SERVICES.xsuaa[0].credentials.clientsecret | cut -d '"' -f 2) | |
password=$(bw get password universalid.sap.com --raw) | |
username=$(bw get username universalid.sap.com --raw) | |
#echo $btp_oauth_url | |
#echo ${client_id}:${client_secret} | |
base64Auth=$(echo -n ${client_id}:${client_secret} | base64) | |
# echo ${base64Auth} | |
curl \ | |
-X POST ${btp_oauth_url} \ | |
-H 'Authorization: Basic '${base64Auth} \ | |
--header 'Content-Type: application/x-www-form-urlencoded' \ | |
--data-urlencode 'grant_type=password' \ | |
--data-urlencode 'username='${username} \ | |
--data-urlencode 'password='${password} \ | |
--data-urlencode 'response_type=token' \ | |
| jq . | |
# | jq .access_token | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment