Introduction
This blog provides a code example, how to use bearer token authentication with RestClient(RestSharp) for bearer authentication(bearer token oauth) in C#.
Getting Started
Here in the below there are two examples which send request to API get and post method using ReestClient(RestSharp). The request format is JSON format and in the header bearer token is included for bearer authentication.
Bearer Token Example
string token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImppYk5ia0ZTU2JteFBZck45Q0ZxUms0SzRndyIsImtpZCI6ImppYk5ia0ZTU2JteFBZck45Q0ZxUms0SzRndyJ9.eyJhdWQiOiJhcGk6Ly9jMTFjMzQzNi0wMjk4LTQ3NTMtYjAzNC1lZDhjMDg3YjRkZTgiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC8xMzFmZDcxZi0xOGVmLTQ0ODMtYjA0Mi0yZWQ1NDBiMjBhODUvIiwiaWF0IjoxNjAwODQwMjUyLCJuYmYiOjE2MDA4NDAyNTIsImV4cCI6MTYwMDg0NDE1MiwiYWlvIjoiRTJCZ1lGajN2bkZSMDQ0MjN4bE11cjhuU3E5K0FnQT0iLCJhcHBpZCI6ImMxMWMzNDM2LTAyOTgtNDc1My1iMDM0LWVkOGMwODdiNGRlOCIsImFwcGlkYWNyIjoiMSIsImlkcCI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzEzMWZkNzFmLTE4ZWYtNDQ4My1iMDQyLTJlZDU0MGIyMGE4NS8iLCJvaWQiOiJlMGVjMTJmNy1jYjk4LTQxZGMtOTU2NC1kNjdiNjAyMDY0MTkiLCJyaCI6IjAuQUFBQUg5Y2ZFLThZZzBTd1FpN1ZRTElLaFRZMEhNR1lBbE5Ic0RUdGpBaDdUZWdLQUFBLiIsInN1YiI6ImUwZWMxMmY3LWNiOTgtNDFkYy05NTY0LWQ2N2I2MDIwNjQxOSIsInRpZCI6IjEzMWZkNzFmLTE4ZWYtNDQ4My1iMDQyLTJlZDU0MGIyMGE4NSIsInV0aSI6IkNqbl95N2VQWFVxUUR1REFQeExBQVEiLCJ2ZXIiOiIxLjAifQ.emGSXtK4lzERav1v2_K6nZlmAevsGzHhObUMlJdhOmQVZqcX76sWlLcRxmvh-Sw0JBj9ya0n1fTGFklijfLyMxUVIKAIgs5pKUpAkDzz0E6qbQWKTf89yy7w7qKAdQnLySzxKdg672dsAOCeDga2FvbHAKWtyKpA9YdmkQJE0rqF8uUwrBRxTpjOUkQkz7XqFCBde2FxmDGHAp_75Yh9za8Ma0XSwi90gZdjNfasX2rxelScyLWjrb8kTmjZ_ZxYgKOqCSUqhw8swvYnlfpN8qknXXQGFnZBqQV3nXqF45vXIcF0HxoOo9gw-mo58FfoZiILH4KucVA2NDVXyRjURw";
Bearer Token Authentication with GET method
The following code example calls the API’s get method using RestClient and includes the bearer token in the request header
IRestRequest request = new RestRequest(Method.GET);
request.AddHeader("content-type", "application/json");
request.AddHeader("content-Length","0");
request.AddHeader("Authorization", "Bearer " + token);
request.RequestFormat = DataFormat.Json;
request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);
RestClient client = new RestClient("url");
IRestResponse response = client.Execute(request);
string responsevalue = response.Content;
Bearer Token Authentication
Bearer Token Authentication with POST method
The following code example calls the API’s POST method using RestClient and includes the bearer token in the request header
IRestRequest request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddHeader("content-Length","0");
request.AddHeader("Authorization", "Bearer " + token);
request.RequestFormat = DataFormat.Json;
request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);
RestClient client = new RestClient("url");
IRestResponse response = client.Execute(request);
string responsevalue = response.Content;
Bearer Token Authentication
Summary
In this blog, we learn how to request API with bearer token authentication using restclient. I hope you have enjoyed it a lot.
Thanks
No comments:
Post a Comment