Q:

How to get / generate access token from azure OAuth 2.0 token (v2) endpoints?

I want to get an access token from given URL: 
https://login.microsoftonline.com/{AzureTenantId}/oauth2/v2.0/token

I am passing the following parameters, as mentioned int he Microsoft docs:
client_id, scope, client_secret, grant_type.
When I hit this URL, I get a "400 Bad Request" response.
When I try the same from Postman, it succeeds and provides me an access token:

But not from my code:
public async Task<string> GetAuthorizationToken(string clientId, string ServicePrincipalPassword, string AzureTenantId) {
            var result = "";
            var requestURL = "https://login.microsoftonline.com/{AzureTenantId}/oauth2/v2.0/token";
            var _httpClient = new HttpClient();

            var model = new {
                client_id = clientId,
                scope = "{clentID}/.default",
                client_secret = ServicePrincipalPassword,
                grant_type = "client_credentials"
            };

            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(model), System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");

            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(requestURL)) {
                Content = httpContent
            };

            using (var response = await _httpClient.SendAsync(httpRequestMessage)) {
                if (response.IsSuccessStatusCode) {
                    var responseStream = await response.Content.ReadAsStringAsync();
                    return result;
                } else {
                    return result;
                }
            }

A:

Your http request format is not correct , try :
var _httpClient = new HttpClient();

var content = new FormUrlEncodedContent(new Dictionary<string, string> {
              { "client_id", "ClientID" },
              { "client_secret", "YourSecret" },
              { "grant_type", "client_credentials" },
              { "scope", "https://graph.microsoft.com/.default" },
            });

var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(requestURL))
{
    Content = content
};

using (var response = await _httpClient.SendAsync(httpRequestMessage))
{
    if (response.IsSuccessStatusCode)
    {
        var responseStream = await response.Content.ReadAsStringAsync();
        return result;
    }
    else
    {
        return result;
    }
}

