我正在尝试使用 RESTsharp 库对 RESTful 服务 (sabre REST api) 进行身份验证,但我无法对其进行身份验证。我正在使用我的客户 ID 和密码。请告诉我如何使用 oAuth 2.0 身份验证器进行身份验证。
我试过这段代码。 (saber 使用的是 OAuth 2.0 认证)
public ActionResult Index()
{
var client = new RestClient("https://api.test.sabre.com");
client.Authenticator = new HttpBasicAuthenticator("myclientid", "myclientsecret");
RestRequest request = new RestRequest("/v1/auth/token", Method.POST);
request.AddHeader("Authorization", "Basic " + client);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "client_credentials");
IRestResponse response = client.Execute(request);
var content = response.Content;
ViewBag.R = content;
return View();
}
我得到了这个结果
{"error":"invalid_client","error_description":"Credentials are missing or the syntax is not correct"}
请告诉我做错了什么。 谢谢
显示运行代码(不使用 RestSharp)和使用 RestSharp 的代码的 Fiddler 比较快照
使用 RestSharp
最佳答案
在我看来,您添加了两次 Authorization header 。 documentation here说
The authenticator’s Authenticate method is the very first thing called upon calling RestClient.Execute
查看 HttpBasicAuthenticator 的实现,Authenticate 方法将适当的 header 添加到请求中。
因此从您的示例中删除以下行:
request.AddHeader("Authorization", "Basic " + client);
关于authentication - RestSharp 中的 OAuth 2.0 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31352153/