I am hoping to write a back-end utility in .NET using OAuth Client Credentials Login flow (there is no user but I do have a client id and secret to retrieve an access token). I have looked here but there is not a code example for .NET.
I have written what i believe to be the equivalent .NET code but it's not working. Please could you advise? Code is below.
I have done the equivalent using the SDK and this works fine so I know there must be a way without using the SDK. I have been told that going direct to the API will be more stable than using the SDK because you have to recompile the SDK when the schema changes (even if it changes in an area that I am not using).
private async Task<string> Post_Request_Response()
{
// HttpClient Client = new HttpClient();
// public const string host = "mypurecloud.ie";
//set up client credentials
string credentials = String.Format("{0}:{1}", AccessToken.ClientId, AccessToken.ClientSecret);
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("client_credentials"
, Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));
var values = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
};
var content = new FormUrlEncodedContent(values);
var response = await Client.PostAsync("https://login." + host + "/oauth/token", content);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}