The /token
endpoint returns a payload to help you figure out what part of the request it didn't like:
WebRequest
is an older API for doing HTTP requests - the standard approach for modern C# is to use HttpClient
, which by default doesn't throw when the server returns a non-success HTTP status code, which is handy if you're trying to read the response body of a failed request:
https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-7.0
But if need to use WebRequest
you'll need to wrap your .GetResponse()
line in a try/catch:
try {
// ...
var response = (HttpWebResponse)request.GetResponse();
// ...
} catch (WebException ex) {
Console.WriteLine(ex.Status);
Console.WriteLine(ex.Response);
}