调用时使用 JWT 承载身份验证 API
Using JWT Bearer Authentication When Call API
我想访问已购买产品的 API,我根据产品文档使用了以下代码:
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);
文档说:
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>
如何在上层请求中添加JWT认证?
request.AddHeader("Authorization", $"Bearer {Token}");
根据 documentation you've provided 的 Access Granted Client Credentials 部分,您需要为 Bearer 令牌调用的端点是 /admin/api/index.php/api/login
。
使用正确的凭据调用该端点将使用以下 JSON:
填充您的 response.Content
{
"status": 200,
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9kZW1vNC5zYXNyYWRpdXMuY29tXC9hZG1pblwvYXBpXC9pbmRleC5waHBcL2FwaVwvbG9naW4iLCJpYXQiOjE2MzA0MDU5OTgsImV4cCI6MTYzMDQwOTU5OCwibmJmIjoxNjMwNDA1OTk4LCJqdGkiOiJCZmdvN00zN2pkbGtRRzFhIiwic3ViIjoxLCJwcnYiOiJkNzk3N2M0N2U5MTY5NjUxMDEwNzM0ZDJmYmY4Y2MxMzlmM2U1MDM0In0.7tNWgF6psOPKpPC9-zU_hEK_GLx3-BeFlIW9LE4wzYo"
}
将上述 JSON 对象反序列化为令牌对象,token
字段将成为您的 JWT 令牌。
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;
对于后续请求,要进行身份验证,请添加此行 & 你应该准备好了。
request.AddHeader("Authorization", $"Bearer {token}");
我想访问已购买产品的 API,我根据产品文档使用了以下代码:
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);
文档说:
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>
如何在上层请求中添加JWT认证?
request.AddHeader("Authorization", $"Bearer {Token}");
根据 documentation you've provided 的 Access Granted Client Credentials 部分,您需要为 Bearer 令牌调用的端点是 /admin/api/index.php/api/login
。
使用正确的凭据调用该端点将使用以下 JSON:
填充您的response.Content
{
"status": 200,
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9kZW1vNC5zYXNyYWRpdXMuY29tXC9hZG1pblwvYXBpXC9pbmRleC5waHBcL2FwaVwvbG9naW4iLCJpYXQiOjE2MzA0MDU5OTgsImV4cCI6MTYzMDQwOTU5OCwibmJmIjoxNjMwNDA1OTk4LCJqdGkiOiJCZmdvN00zN2pkbGtRRzFhIiwic3ViIjoxLCJwcnYiOiJkNzk3N2M0N2U5MTY5NjUxMDEwNzM0ZDJmYmY4Y2MxMzlmM2U1MDM0In0.7tNWgF6psOPKpPC9-zU_hEK_GLx3-BeFlIW9LE4wzYo"
}
将上述 JSON 对象反序列化为令牌对象,token
字段将成为您的 JWT 令牌。
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;
对于后续请求,要进行身份验证,请添加此行 & 你应该准备好了。
request.AddHeader("Authorization", $"Bearer {token}");