Reputation: 74
I am refreshing a data model in azure analysis services using a rest api.
Refrence doc: https://learn.microsoft.com/en-us/azure/analysis-services/analysis-services-async-refresh#base-url
I am getting a HTTPError 400 Client Error: Bad Request for url. What is the correct way to format the request?
import requests
from azure.identity import DefaultAzureCredential
# URL for the Azure Analysis Services REST API
url = "https://uksouth.asazure.windows.net/servers/myserver/models/mymodel/refreshes"
# get token
resource_uri = "https://uksouth.asazure.windows.net"
credential = DefaultAzureCredential()
token = credential.get_token(resource_uri)
access_token = token.token
# Prepare the headers
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
}
# Prepare the body for a full refresh
body = {
"Type": "Full"
}
# Make the POST request
try:
response = requests.post(url, headers=headers, json=body)
response.raise_for_status()
print("Model refresh triggered successfully")
except Exception as e:
print(f"Failed to trigger model refresh: {e}")
raise e
Full error
HTTP error occurred: 400 Client Error: Bad Request for url: https://uksouth.asazure.windows.net/servers/myserver/models/mymodel/refreshes Response Status Code: 400 Response Text: {"code":"Unauthorized","subCode":0,"message":"An internal error occurred.","timeStamp":"2024-01-04T13:42:01.4233221Z","httpStatusCode":400
Upvotes: 0
Views: 380
Reputation: 212
There are two things that you may want to check.
Which type of credential you expect to work. DefaultAzureCredential is a chain of different types of credentials. If none of them works, you will not be able to get a valid token. https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/README.md#defaultazurecredential
The scope, credential.get_token(scope) is used to get the token and you need to specify the correct scope string to let the server know which resource you want to access. e.g. the scope for accessing ARM resources is "https://management.azure.com/.default".
Hope this helps.
(I work in Azure SDK team in Microsoft)
Upvotes: 1