如何在 RestSharp 中使用 OAuth2
How to use OAuth2 in RestSharp
在服务器端整理 OAuth2 几天后 (Spring java),我开始研究用 C# 编写的客户端。我正在使用 RestSharp 调用我的网络 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);
我只是 运行 这段代码处于调试模式,当我查看响应时我得到了授权。
当我使用相同的参数在控制台上执行 curl 时,它工作正常,但我似乎无法使其在 C# 中工作。这是 curl 命令:
curl -H "Accept: application/json" client-app:secret@example.com/myapi/oauth/token -d grant_type=client_credentials
顺便说一下,我已经用占位符替换了我真实的 API 网址和其他信息。
见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
你的URL请求
curl -H "Accept: application/json" \
-d grant_type=client_credentials \
client-app:secret@example.com/myapi/oauth/token
你的 cURL 命令有效的原因
默认 Content-Type
(如果未指定)和 POST(使用 -d
开关时的默认值)是 application/x-www-form-urlencoded
默认身份验证类型,如果未指定,则为 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
指定身份验证类型
您可以在 cURL 命令中使用 -v
开关来查看请求中涉及的所有 header。
RestSharp 修复:
将Content-Type
设置为application/x-www-form-urlencoded
添加基本身份验证
client.Authenticator = new HttpBasicAuthenticator("client-app", "secret");
去掉
request.AddParameter("client_id", "client-app");
request.AddParameter("client_secret", "secret");
将Accept
header设为application/json
我能够使用以下两个功能。
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>
///
/// </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);
}
在服务器端整理 OAuth2 几天后 (Spring java),我开始研究用 C# 编写的客户端。我正在使用 RestSharp 调用我的网络 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);
我只是 运行 这段代码处于调试模式,当我查看响应时我得到了授权。
当我使用相同的参数在控制台上执行 curl 时,它工作正常,但我似乎无法使其在 C# 中工作。这是 curl 命令:
curl -H "Accept: application/json" client-app:secret@example.com/myapi/oauth/token -d grant_type=client_credentials
顺便说一下,我已经用占位符替换了我真实的 API 网址和其他信息。
见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
你的URL请求
curl -H "Accept: application/json" \
-d grant_type=client_credentials \
client-app:secret@example.com/myapi/oauth/token
你的 cURL 命令有效的原因
默认
Content-Type
(如果未指定)和 POST(使用-d
开关时的默认值)是application/x-www-form-urlencoded
默认身份验证类型,如果未指定,则为 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
您可以在 cURL 命令中使用 -v
开关来查看请求中涉及的所有 header。
RestSharp 修复:
将
Content-Type
设置为application/x-www-form-urlencoded
添加基本身份验证
client.Authenticator = new HttpBasicAuthenticator("client-app", "secret");
去掉
request.AddParameter("client_id", "client-app"); request.AddParameter("client_secret", "secret");
将
Accept
header设为application/json
我能够使用以下两个功能。
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>
///
/// </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);
}