Files
docs-harvester/get-token.py
Slawomir Koszewski 495ba0b0b3
All checks were successful
/ unit-tests (push) Successful in 13s
Modernized the get-token.py.
2025-11-09 10:52:22 +01:00

50 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Get an Azure DevOps token and print it in a format suitable for exporting as an environment variable.
Usage:
eval $(python get-token.py)
or
python get-token.py > set-ado-token.sh
source set-ado-token.sh
Now you can use the ADO_TOKEN environment variable, for example using curl:
curl -sH "Authorization: Bearer $ADO_TOKEN" "https://dev.azure.com/$ADO_ORGANIZATION_URL/_apis/projects?api-version=7.1"
"""
from sk.azure import get_token
from argparse import ArgumentParser
args = ArgumentParser(description="Get Azure DevOps token and print it for exporting as environment variable.")
args.add_argument("--tenant-id", type=str, required=True, help="Azure AD Tenant ID")
args.add_argument("--client-id", type=str, required=True, help="Azure AD Client ID")
args.add_argument("--pem-path", type=str, help="Path to PEM file for authentication (optional)")
args.add_argument("--client-secret", type=str, help="Client Secret for authentication (optional)")
args = args.parse_args()
if args.pem_path:
token = get_token(
tenant_id=args.tenant_id,
client_id=args.client_id,
pem_path=args.pem_path
)
elif args.client_secret:
if not args.client_secret:
print("Client secret file is empty.")
exit(1)
token = get_token(
tenant_id=args.tenant_id,
client_id=args.client_id,
client_secret=args.client_secret
)
else:
token = get_token(
tenant_id=args.tenant_id,
client_id=args.client_id
)
print(f"export ADO_TOKEN='{token}'")