Puedes usar la librería RestSharp, para instalarla usa el siguiente comando en consola administración de paquetes
Install-Package RestSharp -Version 106.3.1
using RestSharp;
using RestSharp.Authenticators;
using System.Collections;
using System.Dynamic;
//Con este metodo obtienes el token
public string getToken(String sURL, String sUserName, String sPassword)
{ String access_token = "";
string clientId = "client";
string clientSecret = "secret";
string credentials = String.Format("{0}:{1}", clientId, clientSecret);
RestClient restClient = new RestClient(sURL);
RestRequest restRequest = new RestRequest("/oauth/token");
restRequest.RequestFormat = DataFormat.Json;
restRequest.Method = Method.POST;
restRequest.AddHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));
restRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
restRequest.AddHeader("Accept", "application/json");
restRequest.AddParameter("grant_type", "password");
restRequest.AddParameter("username", sUserName);
restRequest.AddParameter("password", sPassword);
try {
var response = restClient.Execute(restRequest);
if (response.StatusCode == HttpStatusCode.BadRequest){
dynamic objError = new ExpandoObject();
objError = JsonConvert.DeserializeObject(response.Content);
access_token = "";
var error = objError.invalid_grant;
var error_description = objError.error_description;
MessageBox.Show(error + " - " + error_description, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (response.StatusCode == HttpStatusCode.Accepted || response.StatusCode == HttpStatusCode.OK){
dynamic objRpta = new ExpandoObject();
objRpta = JsonConvert.DeserializeObject(response.Content);
access_token = objRpta.access_token;
String token_type = objRpta.token_type;
int expires_in = objRpta.expires_in;
}
}
catch(IOException e){
MessageBox.Show(e.ToString(), "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return access_token;
}