question

CliveGargan-2554 avatar image
0 Votes"
CliveGargan-2554 asked XiaopoYang-MSFT edited

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

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-supported
image.png (61.7 KiB)
image.png (127.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers