I am getting this error when I try to run this code from C#:
{
"error": "invalid_request",
"error_description": "AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: e605b59b-ce96-443a-be34-a80c00b84f00\r\nCorrelation ID: d6a891a8-4aed-485c-939d-95a377ca1c3d\r\nTimestamp: 2023-07-21 11:14:38Z",
"error_codes": [
900144
],
"timestamp": "2023-07-21 11:14:38Z",
"trace_id": "e605b59b-ce96-443a-be34-a80c00b84f00",
"correlation_id": "d6a891a8-4aed-485c-939d-95a377ca1c3d",
"error_uri": "https://login.microsoftonline.com/error?code=900144"
}
This is the code:
private static async Task<string> GetAccessTokenAsync(string tenantId, string azureClientId, string code, string azureRedirectURI, string azureResourceURI)
{
using (var httpClient = new HttpClient())
{
var tokenEndpoint = $"https://login.microsoftonline.com/{tenantId}/oauth2/token";
var requestBody = $"grant_type=authorization_code&client_id={azureClientId}&code={code}&redirect_uri={azureRedirectURI}&resource={azureResourceURI}";
var content = new StringContent(requestBody, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(tokenEndpoint, content);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
// Assuming the response contains a JSON string
// You may want to use a JSON serializer to deserialize the responseContent.
// For simplicity, we're assuming it's a JSON string.
// You can use NewtonSoft.Json.JsonConvert.DeserializeObject to deserialize the JSON.
return responseContent;
}
else
{
// Handle the error if needed
// You can check response.StatusCode and response.ReasonPhrase for more details.
throw new Exception($"Request failed with status code: {response.StatusCode}, Reason: {response.ReasonPhrase}");
}
}
}
How to resolve this issue before running this code any prerequisite check is there? Can anyone help to resolve this issue?
I am trying to acquire the access token from AVD portal to AutoConnect a session