Generating an Access Token in C# (Response is Unauthorised)

Clive Gargan 1 Reputation point
2021-10-07T15:54:32.473+00:00

I'm trying to generate a token to connect to a third party API as per below instructions:
138596-image.png

138605-image.png

Below is my C# code. When I run it the reponse states unauthorised. Any ideas why? Using the same credentials I can connect via Postman.

        RestClient client = new RestClient("https://ourcompany.pentanarpm.uk/cpmweb/oauth/token");  
        RestRequest request = new RestRequest() { Method = Method.POST };  

        var plainTextBytes = System.Text.Encoding.UTF8.GetBytes("api_client:9517713b-eef2-40fc-ab3c-473c1b8129aa");  
        string userNamePasswordEncodedBase64 = System.Convert.ToBase64String(plainTextBytes);  

        //Add Headers  
        request.AddHeader("Accept", "application/json");  
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");  
        request.AddHeader("Authorization", "Basic {" + userNamePasswordEncodedBase64 + "}");  


        var body = new  
        {  
            grant_type = "password",  
            username = "ourcompany.Pentana.api",  
            Password = "bcbbe8c0-e2e8-4d92-9c0a-be954517742f"  
        };  

        request.AddJsonBody(body);  

        IRestResponse token = null;  
        var response = client.Execute(request);  
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,986 questions
0 comments No comments
{count} votes