0

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?

3
  • Can you please link the documentation? You very likely will not be able to generate a JWT Bearer token using a username and password as that is more suited for Basic authentication. You will most likely need to call another auth endpoint first using your username and password, server will generate a bearer token, returns it and then you can use the token in subsequent requests to authorise your requests. Commented Oct 3, 2021 at 10:14
  • @ErmiyaEskandary Here's the documentation, Please don't to select the c# from language list documenter.getpostman.com/view/11765341/U16byA2y Commented Oct 3, 2021 at 10:18
  • Thanks Abdulsalam! That helps, I've posted an answer - let me know if it puts you on the right track? Commented Oct 3, 2021 at 10:28

2 Answers 2

1

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:

{
  "status": 200,
  "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9kZW1vNC5zYXNyYWRpdXMuY29tXC9hZG1pblwvYXBpXC9pbmRleC5waHBcL2FwaVwvbG9naW4iLCJpYXQiOjE2MzA0MDU5OTgsImV4cCI6MTYzMDQwOTU5OCwibmJmIjoxNjMwNDA1OTk4LCJqdGkiOiJCZmdvN00zN2pkbGtRRzFhIiwic3ViIjoxLCJwcnYiOiJkNzk3N2M0N2U5MTY5NjUxMDEwNzM0ZDJmYmY4Y2MxMzlmM2U1MDM0In0.7tNWgF6psOPKpPC9-zU_hEK_GLx3-BeFlIW9LE4wzYo"
}

Deserialise the above JSON object to a token object & the token field will be your JWT token.

Token.cs

public class Token
{
    public int status { get; set; }
    public string token { get; set; }
}
var tokenObj = JsonConvert.DeserializeObject<Token>(response.Content);
string token = tokenObj.token;

For subsequent requests, to authenticate, add this line & you should be good to go.

request.AddHeader("Authorization", $"Bearer {token}");

Sign up to request clarification or add additional context in comments.

2 Comments

I got "Internal Server Error"
@AbdulsalamElsharif I can't help with more information but is your URL correct? Are your username and passwords correct? Your original question on how to set a bearer token on a RestSharp request has been answered.
0
request.AddHeader("Authorization", $"Bearer {Token}");

2 Comments

How can I get the Token?
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.