0

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

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Sangeetha
  • 1
  • 1
  • 1
    You might want to try with [`FormUrlEncodedContent`](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.formurlencodedcontent?view=net-7.0) instead of `StringContent` class – Mathias R. Jessen Jul 21 '23 at 13:53
  • Does this answer your question? [How to POST using HTTPclient content type = application/x-www-form-urlencoded](https://stackoverflow.com/questions/43158250/how-to-post-using-httpclient-content-type-application-x-www-form-urlencoded) – Charlieface Jul 21 '23 at 14:29
  • 1
    But what does [tag:powershell] have to do with this? Side note: you need to dispose your objects – Charlieface Jul 21 '23 at 14:29
  • is there a way to download the list of application available in the workspace using the c# what all the fields required to achieve this – Sangeetha Jul 26 '23 at 12:25

0 Answers0