Legacy Dev Forum Posts

 View Only

Sign Up

C# Attempted API Calls all receive 400 Bad Request

  • 1.  C# Attempted API Calls all receive 400 Bad Request

    Posted 06-05-2025 18:29

    corcoranj | 2024-01-17 21:47:12 UTC | #1

    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.


    tim.smith | 2024-01-17 21:59:55 UTC | #2

    What's the response body payload? There's usually details in it that tell you what was wrong with your request.

    You might consider using the .NET SDK. It has helpers for auth and provides structured methods for each API endpoint. https://developer.genesys.cloud/devapps/sdk/docexplorer/pureclouddotnet/


    corcoranj | 2024-01-17 22:46:29 UTC | #3

    corcoranj, post:1, topic:24167
    {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
    }}

    Tim,

    This is all I am getting back when I look at the response object:

    {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
    }}

    I have looked at trying to use the SDK but I am unable to get past the Authentication with the SDK. I keep getting errors so I decided to go back to just using the standard HttpClient.

    Does that help at all?


    tim.smith | 2024-01-18 15:08:50 UTC | #4

    corcoranj, post:3, topic:24167
    Content: System.Net.Http.StreamContent

    You need to read the stream. It's been too long since I've done .NET to tell you how to do that off the top of my head, but it should be fairly simple to read that to a string.


    tim.smith | 2024-01-18 15:14:38 UTC | #5

    corcoranj, post:3, topic:24167
    but I am unable to get past the Authentication with the SDK. I keep getting errors

    What errors specifically?


    system | 2024-02-18 15:14:42 UTC | #6

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 24167