Basic Authentication Token

Kailash Chandra Behera | Wednesday, October 07, 2020

Introduction

This blog provides a code example, how to use basic authentication token with RestClient(RestSharp) for oauth basic authentication(oauth basic) 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 basic token is included for basic authentication.

Token Example

 string token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImppYk5ia0ZTU2JteFBZck45Q0ZxUms0SzRndyIsImtpZCI6ImppYk5ia0ZTU2JteFBZck45Q0ZxUms0SzRndyJ9.eyJhdWQiOiJhcGk6Ly9jMTFjMzQzNi0wMjk4LTQ3NTMtYjAzNC1lZDhjMDg3YjRkZTgiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC8xMzFmZDcxZi0xOGVmLTQ0ODMtYjA0Mi0yZWQ1NDBiMjBhODUvIiwiaWF0IjoxNjAwODQwMjUyLCJuYmYiOjE2MDA4NDAyNTIsImV4cCI6MTYwMDg0NDE1MiwiYWlvIjoiRTJCZ1lGajN2bkZSMDQ0MjN4bE11cjhuU3E5K0FnQT0iLCJhcHBpZCI6ImMxMWMzNDM2LTAyOTgtNDc1My1iMDM0LWVkOGMwODdiNGRlOCIsImFwcGlkYWNyIjoiMSIsImlkcCI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzEzMWZkNzFmLTE4ZWYtNDQ4My1iMDQyLTJlZDU0MGIyMGE4NS8iLCJvaWQiOiJlMGVjMTJmNy1jYjk4LTQxZGMtOTU2NC1kNjdiNjAyMDY0MTkiLCJyaCI6IjAuQUFBQUg5Y2ZFLThZZzBTd1FpN1ZRTElLaFRZMEhNR1lBbE5Ic0RUdGpBaDdUZWdLQUFBLiIsInN1YiI6ImUwZWMxMmY3LWNiOTgtNDFkYy05NTY0LWQ2N2I2MDIwNjQxOSIsInRpZCI6IjEzMWZkNzFmLTE4ZWYtNDQ4My1iMDQyLTJlZDU0MGIyMGE4NSIsInV0aSI6IkNqbl95N2VQWFVxUUR1REFQeExBQVEiLCJ2ZXIiOiIxLjAifQ.emGSXtK4lzERav1v2_K6nZlmAevsGzHhObUMlJdhOmQVZqcX76sWlLcRxmvh-Sw0JBj9ya0n1fTGFklijfLyMxUVIKAIgs5pKUpAkDzz0E6qbQWKTf89yy7w7qKAdQnLySzxKdg672dsAOCeDga2FvbHAKWtyKpA9YdmkQJE0rqF8uUwrBRxTpjOUkQkz7XqFCBde2FxmDGHAp_75Yh9za8Ma0XSwi90gZdjNfasX2rxelScyLWjrb8kTmjZ_ZxYgKOqCSUqhw8swvYnlfpN8qknXXQGFnZBqQV3nXqF45vXIcF0HxoOo9gw-mo58FfoZiILH4KucVA2NDVXyRjURw";  

Basic auth Token with GET method

The following code example calls the API’s get method using RestClient and includes the oauth basic 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", "Basic " + 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;  

oauth basic authentication

Basic auth Token with POST method

The following code example calls the API’s POST method using RestClient and includes the oauth basic 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", "Basic  " + 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;  

oauth basic authentication

Summary

In this blog, we learn how to request API with basic token authentication using restclient. I hope you have enjoyed it a lot.

Thanks