我正在使用服务主体对 Azure 进行身份验证,并且我想将 shell 脚本转换为 Java。我的 shell 脚本代码基本上是这样做的:
export AAD_ACCESS_TOKEN=$(az account get-access-token --query accessToken -o tsv)
export ACR_REFRESH_TOKEN=$(curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=access_token&service=$REGISTRY&access_token=$AAD_ACCESS_TOKEN" \
https://$REGISTRY/oauth2/exchange \
| jq '.refresh_token' \
| sed -e 's/^"//' -e 's/"$//')
echo "ACR Refresh Token obtained."
# Create the repo level scope
SCOPE="repository:$REPOSITORY:pull"
# to pull multiple repositories passing in multiple scope arguments.
#&scope="repository:repo:pull,push"
export ACR_ACCESS_TOKEN=$(curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=refresh_token&service=$REGISTRY&scope=$SCOPE&refresh_token=$ACR_REFRESH_TOKEN" \
https://$REGISTRY/oauth2/token \
| jq '.access_token' \
| sed -e 's/^"//' -e 's/"$//')
echo "ACR Access Token obtained."
我正在尝试为
az account get-access-token --query accessToken -o tsv
和
curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=access_token&service=$REGISTRY&access_token=$AAD_ACCESS_TOKEN" https://$REGISTRY/oauth2/exchange | jq '.refresh_token' | sed -e 's/^"//' -e 's/"$//'
和
curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=refresh_token&service=$REGISTRY&scope=$SCOPE&refresh_token=$ACR_REFRESH_TOKEN" https://$REGISTRY/oauth2/token | jq '.access_token' | sed -e 's/^"//' -e 's/"$//'
但还没有真正找到有关如何使用 Java 执行此操作的任何文档。我发现了这一点:https ://github.com/AzureAD/azure-activedirectory-library-for-java/wiki/Acquire-tokens用于获取 AAD 令牌,但该页面上没有任何内容告诉我如何使用服务主体来执行此操作。