Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing out PAT tokens can hit required endpoints they need to get to in Azure DevOps.
#!/usr/bin/env python
import requests
import sys
import pprint
import base64
def encode_string(token):
to_encode_string = "Basic:" + token
encoded_str = base64.b64encode(to_encode_string.encode()).decode('utf-8')
return encoded_str
def send_request(endpoint, encoded_str):
try:
auth_header = 'Basic ' + encoded_str
r = requests.get(endpoint, headers={'Authorization' : auth_header})
result = r.json()
if result:
print("Endpoint " + endpoint + " authorization successful.")
except:
print("Something failed sending request to endpoint " + url + ". Check your PAT token and api endpoints.")
def main():
if len(sys.argv) == 2:
token = sys.argv[1]
encoded_str = encode_string(token)
# Add your required endpoints here. Projects is here as an example.
urls = [
'https://dev.azure.com/{YOUR_ADO_ORGANISATION}/_apis/projects?api-version=5.1'
]
for url in urls:
try:
result = send_request(url, encoded_str)
except:
print("Something failed for endpoint " + url + ". Check the scope of the PAT token and available organisations.")
else:
print("Usage: python " + sys.argv[0] + " RAW_PAT_TOKEN")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment