I want to access an API for a purchased product, I used the following code according to the product documentation:
var client = new RestClient("http://example.com/api/login");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddParameter("username", "admin");
request.AddParameter("password", "admin");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
The documentation says:
Authentication is performed via JWT Bearer
Authentication. Every endpoint requires authentication, so you will
need to add the following header to each request
Authorization: Bearer <JWT>
How can I add the JWT authentication in my upper request?
2
Answers
Based on the Access Granted Client Credentials section of the documentation you’ve provided, the endpoint you need to be calling for a Bearer token is
/admin/api/index.php/api/login
.Calling that endpoint with correct credentials will populate your
response.Content
with the below JSON:Deserialise the above JSON object to a token object & the
token
field will be your JWT token.Token.cs
For subsequent requests, to authenticate, add this line & you should be good to go.
request.AddHeader("Authorization", $"Bearer {token}");