RestSharp无法通过Django Rest框架进行身份验证
我使用RestSharp使用Djnago中的API进行基本身份验证。 我已经在Chrome的rest客户端工具中成功地测试了API。但我无法使用RestSharp进行相同的API调用,它总是返回:RestSharp无法通过Django Rest框架进行身份验证,rest,django-rest-framework,restsharp,Rest,Django Rest Framework,Restsharp,我使用RestSharp使用Djnago中的API进行基本身份验证。 我已经在Chrome的rest客户端工具中成功地测试了API。但我无法使用RestSharp进行相同的API调用,它总是返回: {"detail": "Authentication credentials were not provided."} 如果我使用设置解除Django中的身份验证:'DEFAULT\u PERMISSION\u CLASSES':('rest\u framework.permissions.Allo
{"detail": "Authentication credentials were not provided."}
如果我使用设置解除Django中的身份验证:'DEFAULT\u PERMISSION\u CLASSES':('rest\u framework.permissions.AllowAny',),
它可以工作,但是没有身份验证,这不是一个解决方案
以下是RestSharp的代码:
var client = new RestClient(host);
client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest(service, Method.GET);
var response = client.Execute(request) as RestResponse;
Console.WriteLine(response.Content);
我不确定这是否需要在Django侧或RestSharp侧进行修复。
RestSharp的请求是否需要任何额外的请求头?
还是Django上的配置?查看您的url映射,
您可能需要以“/”结尾。
例如。
您需要更改以下代码:
request.Resource = "myinfo";
致:
这可能是因为它不能正确处理重定向。使用正确的url应该可以修复它。查看您的url映射,
您可能需要以“/”结尾。
例如。
您需要更改以下代码:
request.Resource = "myinfo";
致:
这可能是因为它不能正确处理重定向。使用正确的url应该可以解决问题。您的问题在于身份验证。您使用的是“会话”类型的身份验证,因此您必须首先登录并存储会话cookie 例如:
private RestClient RestCliente = new RestClient("http://yoursiteurl.com");
private CookieContainer SessionCookie = new CookieContainer();
private void ServiceSession() // once per session only
{
RestRequest login = new RestRequest("/api-auth/login/", Method.POST); // path to your login on rest-framework
RestCliente.Authenticator = new HttpBasicAuthenticator("username", "password");
IRestResponse loginresponse = RestCliente.Execute(login);
if (response.StatusCode == HttpStatusCode.OK)
{
var cookie = loginresponse .Cookies.FirstOrDefault();
SessionCookie.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
}
RestCliente.CookieContainer = SessionCookie;
}
private void InfoRequest()
{
RestRequest infoneeded= new RestRequest("/info/path/?format=json", Method.GET);
IRestResponse response = RestCliente.Execute(infoneeded);
Console.WriteLine(response.Content);
}
然后,您可以简单地请求您的信息
例如:
private RestClient RestCliente = new RestClient("http://yoursiteurl.com");
private CookieContainer SessionCookie = new CookieContainer();
private void ServiceSession() // once per session only
{
RestRequest login = new RestRequest("/api-auth/login/", Method.POST); // path to your login on rest-framework
RestCliente.Authenticator = new HttpBasicAuthenticator("username", "password");
IRestResponse loginresponse = RestCliente.Execute(login);
if (response.StatusCode == HttpStatusCode.OK)
{
var cookie = loginresponse .Cookies.FirstOrDefault();
SessionCookie.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
}
RestCliente.CookieContainer = SessionCookie;
}
private void InfoRequest()
{
RestRequest infoneeded= new RestRequest("/info/path/?format=json", Method.GET);
IRestResponse response = RestCliente.Execute(infoneeded);
Console.WriteLine(response.Content);
}
您的问题在于身份验证的类型。您使用的是“会话”类型的身份验证,因此您必须首先登录并存储会话cookie 例如:
private RestClient RestCliente = new RestClient("http://yoursiteurl.com");
private CookieContainer SessionCookie = new CookieContainer();
private void ServiceSession() // once per session only
{
RestRequest login = new RestRequest("/api-auth/login/", Method.POST); // path to your login on rest-framework
RestCliente.Authenticator = new HttpBasicAuthenticator("username", "password");
IRestResponse loginresponse = RestCliente.Execute(login);
if (response.StatusCode == HttpStatusCode.OK)
{
var cookie = loginresponse .Cookies.FirstOrDefault();
SessionCookie.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
}
RestCliente.CookieContainer = SessionCookie;
}
private void InfoRequest()
{
RestRequest infoneeded= new RestRequest("/info/path/?format=json", Method.GET);
IRestResponse response = RestCliente.Execute(infoneeded);
Console.WriteLine(response.Content);
}
然后,您可以简单地请求您的信息
例如:
private RestClient RestCliente = new RestClient("http://yoursiteurl.com");
private CookieContainer SessionCookie = new CookieContainer();
private void ServiceSession() // once per session only
{
RestRequest login = new RestRequest("/api-auth/login/", Method.POST); // path to your login on rest-framework
RestCliente.Authenticator = new HttpBasicAuthenticator("username", "password");
IRestResponse loginresponse = RestCliente.Execute(login);
if (response.StatusCode == HttpStatusCode.OK)
{
var cookie = loginresponse .Cookies.FirstOrDefault();
SessionCookie.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
}
RestCliente.CookieContainer = SessionCookie;
}
private void InfoRequest()
{
RestRequest infoneeded= new RestRequest("/info/path/?format=json", Method.GET);
IRestResponse response = RestCliente.Execute(infoneeded);
Console.WriteLine(response.Content);
}
谢谢,但这不是我的情况,映射是正确的。也许是服务器配置的问题,它在我的本地版本中使用django的“runserver”效果很好。你能在你的代码示例中发布变量“service”的值吗?我遇到了与您完全相同的问题,我通过在服务中添加“/”解决了这个问题。Django将不带尾随斜杠的请求重定向到附加斜杠的url。Restsharp不会将登录凭据与导致错误的重定向一起转发。谢谢,但这不适用于我的情况,映射是正确的。也许是服务器配置的问题,它在我的本地版本中使用django的“runserver”效果很好。你能在你的代码示例中发布变量“service”的值吗?我遇到了与您完全相同的问题,我通过在服务中添加“/”解决了这个问题。Django将不带尾随斜杠的请求重定向到附加斜杠的url。Restsharp没有通过重定向转发登录凭据,这导致了错误。我在使用selenium时遇到了同样的问题,因此这可能与django发送的请求有关,而不是实际登录。你找到解决这个问题的方法了吗?没有,我改成用python重建所有东西。但是你在@Richard的回答中检查过这个案例吗?这肯定会导致这个问题。我在使用selenium时也遇到同样的问题,所以这可能与django发送的请求有关,而不是实际登录。你找到解决这个问题的方法了吗?没有,我改成用python重建所有东西。但是你在@Richard的回答中检查过这个案例吗?这是一个肯定会导致这个问题的案例。