Background
I am making a restful API call to Microsoft's Azure Consumption endpoint as detailed below.
https://learn.microsoft.com/en-gb/rest/api/consumption/reservationrecommendations/list
However I am always presented with the below error.
Authentication token doesn't have enrollment level access.
{
"error": {
"code": "401",
"message": "Authentication token doesn't have enrollment level access.
}
}
The token is valid and can be used to access the other endpoints under the consumption API. The "Try It" test link on the Azure page actually returns a 200, however when I make the call I get a 401.
Question
Can anyone shed any light on this error message? I cannot find any help on this error anywhere.
Code
Authentication
private static string GetAccessToken(string clientId, string clientSecret, string tenantId)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json;");
string hostname = $"https://login.microsoftonline.com/{tenantId}/oauth2/token";
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
new KeyValuePair<string, string>("resource", "https://management.azure.com/")
});
HttpResponseMessage httpResponse = client.PostAsync(hostname, content).Result;
var responseString = httpResponse.Content.ReadAsStringAsync();
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
dynamic tokenObject = JsonConvert.DeserializeObject(responseString.Result);
return tokenObject.access_token;
}
else
{
return null;
}
}
API Call
public static dynamic GetReservationRecommendations(Params parameters)
{
var token = GetAccessToken(parameters.ClientId, parameters.ClientSecret, parameters.TenantId);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json;");
string hostname = $"https://management.azure.com/subscriptions/{parameters.SubscriptionId}/providers/Microsoft.Consumption/reservationRecommendations?api-version=2018-10-01";
HttpResponseMessage httpResponse = client.GetAsync(hostname).Result;
var responseString = httpResponse.Content.ReadAsStringAsync();
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
return responseString.Result;
}
else
{
return null;
}
}