Click here to Skip to main content
15,672,367 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to push data via rest api which need to be used with a bearer token authentication. I have client_id, client_secret. How am I to do this using c# desktop application

POST /connect/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id= JC-POS-000001&client_secret= S1D+qcjn47M=

What I have tried:

var client = new RestClient($"https://dummy/api/");
           var request = new RestRequest("Create", Method.POST);
           request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
           request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id= CCB1-PS-22-12&client_secret=e4gUOM7nEh==");

           var response = client.Execute(request);
           if (response.StatusCode == System.Net.HttpStatusCode.OK)

Updated 30-Mar-22 23:09pm

1 solution

According to some solutions I've seen posted around you might need to instead use AddParameter() and provide the parameter name and value for each. So instead of:
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id= abc&client_secret=def");

You would do:
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_id", "abc");
request.AddParameter("client_secret", "def");

Try giving this a whirl and see whether it works!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900