1

I am trying to request the usage metrics from a virtual machine I have running on Azure Devops. I know it's online because i've sent a ping. However, every time I try to run the program with the correct Get information filled in it gives me an error:

{"error":{"code":"AuthenticationFailed","message":"Authentication failed. The 'Authorization' header is missing."}}

I am following the instructions here: https://learn.microsoft.com/en-us/azure/virtual-machines/linux/metrics-vm-usage-rest

import requests
BASE_URL = "GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmname}/providers/microsoft.insights/metrics?api-version=2018-01-01&metricnames=Percentage%20CPU&timespan=2018-06-05T03:00:00Z/2018-06-07T03:00:00Z"
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer {myPAT}"
}
response = requests.get(BASE_URL,headers)
print(response.text)

The bug lies in my Authorization header, what am I missing?

Edit: Actually this question Is there a way to call Azure Devops via python using 'requests'? solved my issue but now I have another error "{"error":{"code":"InvalidAuthenticationToken","message":"The access token is invalid."}}". I am reading the docs. https://learn.microsoft.com/en-us/azure/active-directory-b2c/access-tokens Thank you.

2

1 Answer 1

1

Basically, you cannot use the Azure DevOps PAT. You need to Create a Service Principal and Request the Access Token by following this document : Azure REST API Reference

It's easy to use curl to achieve that, please refer to Calling Azure REST API via curl for details.

And as mentioned in the blog, if you need a token just to run some test and you don’t want to go through Service Principal creation, then you can just run below command to get the access token. You’ll get your access token with a maximum validity of 1 hour.

az account get-access-token

After that you can use the access token in your script.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I actually already had a tenant but I did need to install the Azure CLI to use "az".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.