I'm trying GET/POST call API, I've tried this and it works but I can't use RestSharp because it is not compatible with the rest of the application. Is there another way I can use a bearer token?
PS. The Token will never expire so I can pass it as a fixed string.
using System;
using RestSharp;
namespace HelloWorldApplication {
class HelloWorld {
static void Main(string[] args) {
var client = new RestClient("https://test.com/api/");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer myToken");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
}
}
I've tried to use HttpClient too
static async Task Main(string[] args)
{
var endpoint = new Uri("https://test.com/api/'");
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "myToken");
var result = httpClient.GetAsync(endpoint).Result;
var json = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(json);
}