如何在RestSharp中使用OAuth2

时间:2015-05-08 22:46:47

标签: c# rest curl oauth-2.0 restsharp

在服务器端(Spring java)整理OAuth2几天之后,我开始研究用C#编写的客户端。我正在使用RestSharp来调用我的Web API,但我对OAuth2有很大的困难。几乎没有任何文档,我在网上找到的几个例子都不起作用。有人能为我提供最新的代码示例吗?我可以使用吗?

到目前为止,我有以下内容:

var client = new RestClient("http://example.com/myapi/oauth/token");
RestRequest request = new RestRequest() { Method = Method.POST };

request.AddHeader("Content-Type", "application/json");
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_id", "client-app");
request.AddParameter("client_secret", "secret");

var response = client.Execute(request);

我只是在调试模式下运行此代码,当我查看响应时,我未经授权。

当我使用相同的参数在控制台上卷曲时它工作正常,但似乎我不能使它在C#中工作。这是curl命令:

curl -H "Accept: application/json" client-app:secret@example.com/myapi/oauth/token -d grant_type=client_credentials

顺便说一下,我已经用占位符替换了我的真实API网址和其他信息。

2 个答案:

答案 0 :(得分:43)

请参阅RFC 6749 - 4.4.2. Client Credentials - Access Token Request

以下是请求的基本格式

POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials

您的cURL请求

curl -H "Accept: application/json" \
     -d grant_type=client_credentials \
     client-app:secret@example.com/myapi/oauth/token 

你的cURL命令工作的原因

  1. 使用POST时默认Content-Type(如果未指定)(默认使用-d切换时)为application/x-www-form-urlencoded
  2. 默认身份验证类型(如果未指定)为Basic。用户名和密码通过-u选项或URL

    传递
    -u username:password (client-app:secret)
    
    -- or put it in the url --
    
    client-app:secret@example.com/myapi/oauth/token
    

    您还可以使用--basic--digest

  3. 指定身份验证类型

    您可以使用cURL命令中的-v开关查看请求中涉及的所有标头。

    RestSharp 修复:

    1. Content-Type设置为application/x-www-form-urlencoded

    2. 添加基本身份验证

      client.Authenticator = new HttpBasicAuthenticator("client-app", "secret");
      
    3. 摆脱

      request.AddParameter("client_id", "client-app");
      request.AddParameter("client_secret", "secret");
      
    4. Accept标题设置为application/json

答案 1 :(得分:4)

我能够完成以下两个功能。

 public RestClient getClient2(string user, string token)
    {
        RestClient client = new RestClient();
        client.BaseUrl = new Uri(baseUrl);
        client.Authenticator = new HttpBasicAuthenticator(user, token);                
        //client.Authenticator = new OAuth2UriQueryParameterAuthenticator(token); //works
        //client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(token); // doesn't work

        return client;
    }

  public GitHubUser GetGitHubUser2()
    {
        RestRequest request = new RestRequest();        
        request.Resource = "/users/huj";
        request.RootElement = "GitHubUser";

        RestClient client = getClient2(myUser, myToken);

        return Execute<GitHubUser>(client, request);        
    }


    /// <summary>
    /// http://stackoverflow.com/questions/30133937/how-to-use-oauth2-in-restsharp
    /// </summary>
    /// <returns>GitHubUser</returns>
    public GitHubUser GetGitHubUser3()
    {
        //RestRequest request = new RestRequest(Method.POST);  //empty data
        RestRequest request = new RestRequest();
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        request.AddHeader("Accept", "application/json");
        request.AddParameter("grant_type", "client_credentials");

        request.Resource = "/users/huj";
        request.RootElement = "GitHubUser";

        RestClient client = getClient2(myUser, myToken);

        return Execute<GitHubUser>(client, request);
    }