I am very new to Genesys and the API. I have the following below code that I am able to use to authenticate and return an access token but when I attempt to call any of several endpoints I keep getting the following:
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: keep-alive
Inin-Correlation-Id: 59151167-f365-4745-7f1c-52af1d87bfd4
Strict-Transport-Security: max-age=31536000
Vary: Accept-Encoding
Date: Wed, 17 Jan 2024 17:58:02 GMT
Content-Length: 28
Content-Type: application/json
}}
Here is my C# Code to make the request to get the access token and then use that token to make a call to the API:
private void button1_Click(object sender, EventArgs e)
{
var token = this.GetAuthenticationToken();
GetTokenFromCode(token);
}
private string GetAuthenticationToken()
{
//Local Variable Declaration
var returnValue = string.Empty;
var jsonStr = string.Empty;
var byteArray = default(byte[]);
var dataStream = default(System.IO.Stream);
var response = default(WebResponse);
var request = default(WebRequest);
//specify to use TLS 1.2 as default connection
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
//Init the Web Request to the authentication service
request = WebRequest.Create("https://login.usw2.pure.cloud/oauth/token");
//request = WebRequest.Create("https://login.mypurecloud.com/oauth/token");
//Build The JSON Body
jsonStr = "grant_type=client_credentials&client_id=<clientId>&client_secret=<secret>";
//Convert the JSON string into a byte array
byteArray = Encoding.UTF8.GetBytes(jsonStr);
//Set Sender Properties
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
//Init the request stream
dataStream = request.GetRequestStream();
//Write the JSON data to the server
dataStream.Write(byteArray, 0, byteArray.Length);
//Retrieve the response
response = request.GetResponse();
//Read the response
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
{
returnValue = reader.ReadToEnd();
}
if (!string.IsNullOrEmpty(returnValue))
{
var jsonObj = Newtonsoft.Json.Linq.JObject.Parse(returnValue);
returnValue = jsonObj.Property("access_token").Value.ToObject<string>();
}
return returnValue;
}
private string GetTokenFromCode(string code)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", code);
var response = client.GetAsync($"https://api.usw2.pure.cloud/api/v2/users/me").Result;
return string.Empty;
}
Can anyone please help me understand what I am doing wrong and how to make a proper call to the API using the retrieved access token?
Thanks.