authentication - RestSharp 中的 OAuth 2.0 身份验证

标签 authentication oauth restsharp

我正在尝试使用 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

using RestSharp Not using 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/

相关文章:

php - 如何在不使用数据库的情况下仅使用 codeigniter PHP 中的 session token 结果请求进行身份验证?

java - Spring Boot + Spring Security 的自定义登录页面

c# - Twitter OAuth Flow - 对 oob/3-legged auth 和一般流程感到困惑,不需要 PIN?

ios - 如何使用 ASWebAuthenticationSession 获取授权码?

laravel - Httponly cookie 作为不记名 token - Laravel Passport

c# - 是否可以使用 servicestack 上传文件和发布数据?

php - 使用 PHP 对移动设备进行 token 身份验证

python - MSSQL 连接字符串 - 使用另一组凭据的 Windows 身份验证

c# - 如何使用 RestSharp 为不记名 token 实现自定义 IAuthenticationModule

c# - 如果通过主机名访问 API,RestSharp 和 NTLM 身份验证不起作用