Oauth 2.0 发送OAuth令牌在Postman中有效,但在RestSharp中无效
我尝试使用Postman向auth0api发送一个承载令牌,效果非常好 然后我使用RestSharp(在c#中)尝试了同样的方法,但根本不起作用 下面是我的代码。我试过很多不同的格式,但都不管用。。我还有别的办法让它工作吗Oauth 2.0 发送OAuth令牌在Postman中有效,但在RestSharp中无效,oauth-2.0,restsharp,auth0,Oauth 2.0,Restsharp,Auth0,我尝试使用Postman向auth0api发送一个承载令牌,效果非常好 然后我使用RestSharp(在c#中)尝试了同样的方法,但根本不起作用 下面是我的代码。我试过很多不同的格式,但都不管用。。我还有别的办法让它工作吗 var client = new RestClient("http://domain.auth0.com/api/v2/users"); RestRequest request = new RestRequest(Method.GET); //request.AddHead
var client = new RestClient("http://domain.auth0.com/api/v2/users");
RestRequest request = new RestRequest(Method.GET);
//request.AddHeader("authorization", "Bearer eyJhbGcJ9.eyJhdWQiOiJ6VU4hVWUE2.token");
//request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
//request.AddHeader("Accept", "application/json");
//RestClient client = new RestClient("http://domain.auth0.com");
//RestRequest request = new RestRequest("api/v2/users", Method.GET);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Accept", "application/json");
request.AddParameter("Authorization",
string.Format("Bearer " + "eyJhbGciOI1NiIsI9.eyJhdWQiOiWmVhTWpD2VycyI6eyJhY.token"),
ParameterType.HttpHeader);
//request.AddParameter("Authorization",
// String.Format("Bearer {0}", token),
//ParameterType.HttpHeader);
var response = client.Execute(request);
PS:令牌已更改。问题是您使用的是HTTP URL。当您发出第一个请求时,会包含令牌,但您会收到一个重定向响应,通知您应该调用HTTPS端点 由于RestSharp不会在由于第一个重定向响应而自动执行的第二个请求中包含令牌,因此您会得到未经授权的响应 您需要将URL更新为HTTPS,这将阻止重定向,从而解决您的问题。如果要使用同一客户机发出多个经过身份验证的请求,还可以将代码更改为:
using RestSharp;
using RestSharp.Authenticators;
class Program
{
static void Main(string[] args)
{
// Use the HTTPS scheme
var client = new RestClient("https://[domain].auth0.com/api/v2/users");
client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(
"eyJhbGciJIUz.eyJhdWQi4QW5OXhCNTNlNDdjIn0.vnzGPiWA", // Update the token
"Bearer");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Console.WriteLine("{0}", response.StatusCode);
}
}
如果您确实需要处理重定向并且仍然发送令牌,请检查:谢谢您编辑我的问题!:-)谢谢你João:-)当我改成https时。我的状态代码中有一个“错误的请求”。我从Web应用程序改为控制台应用程序,但仍然得到相同的结果。。我应该试试别的吗?我在这里遗漏了什么吗?我用一个有效的令牌对我的Auth0帐户测试了上面的代码,它工作正常。确保使用最新的RestSharp版本和完全相同的代码。如果错误请求继续,请尝试包含有关返回的响应的更多信息。在没有重定向的情况下也会发生这种情况。也许是不同的web前端服务器在集群中工作,我不知道。请求Auth令牌在Postman和RestSharp中都会产生相同的结果,但它只在Postman中工作,原因未知。很抱歉,我错了。重定向到同一资源,但缺少尾随斜杠。问题解决了。谢谢