Skip to content

Instantly share code, notes, and snippets.

@h2floh
Created August 30, 2021 04:10
Show Gist options
  • Save h2floh/42c6534318d2648e5de9e4153253f382 to your computer and use it in GitHub Desktop.
Save h2floh/42c6534318d2648e5de9e4153253f382 to your computer and use it in GitHub Desktop.
Bash script showing how to leverage az login to call Azure DevOps REST endpoints
#!/bin/bash
## Demo script to show how to leverage Azure DevOps CLI Extension to call DevOps REST API directly
## without the need for PAT token
# configuration
YOUR_AZURE_DEV_OPS_ORG=''
YOUR_AZURE_DEV_OPS_PROJECT_NAME=''
# Reverse Engineered this part by looking into Azure DevOps CLI Extension
# https://github.com/Azure/azure-devops-cli-extension/blob/8cf32a41126b2b66f130843d4d16de19290052b9/azure-devops/azext_devops/devops_sdk/client.py#L71
# First get access token for resource 499b84ac-1321-427f-aa17-267ca6975798
access_token=$(az account get-access-token --resource 499b84ac-1321-427f-aa17-267ca6975798 --query 'accessToken' | xargs)
# Wrap it as BASIC Auth Credentials
basic_auth=$(printf ":$access_token" | base64 --wrap=0)
# X Headers are important
curl -v -X POST \
-H "Authorization: Basic $basic_auth" \
-H "X-TFS-FedAuthRedirect: Suppress" \
-H "X-VSS-ForceMsaPassThrough: True" \
-H "Content-Type: application/json" \
-d '{"name": "TEST", "description": "Test Env to demonstrate authentication via AZ CLI"}' \
"https://dev.azure.com/$YOUR_AZURE_DEV_OPS_ORG/$YOUR_AZURE_DEV_OPS_PROJECT_NAME/_apis/distributedtask/environments?api-version=6.1-preview.1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment