我们构建了一个等待来自某些客户端软件的API的API。我们使用Python没有问题GET(ting)和POST(ting)但我们有人试图使用基于RESTSharp库的一些.Net代码来访问它,尽管经过了数小时的搜索,他们似乎无法获得授权。
这是一些有效的Python代码
POST_HEADER = {'Authorization': "Token a_valid_token_that_works_with_python code",
'Content-Type':'application/json'}
BASE_URL = 'https://api.ourendpoint.com/v1/user-tasks/'
#here is the code to POST some data - the authorization token is in POST_HEADER
response = requests.post(BASE_URL, data = JSON_STRING ,headers = POST_HEADER)
效果很好 - 但我们认为是类似的.Net代码不起作用 - 我们收到授权错误 - 因此令牌发送方式出错了
RestClient client = new RestClient("https://api.ourendpoint.com/v1/");
client.AddDefaultHeader("Authorization", "Token a_valid_token_that_works_with_python code");
RestRequest request = new RestRequest("user-tasks", Method.POST);
IRestResponse response = client.Execute(request);
# there is some stuff here to create the data to be POST (ed) but we have
# taken that out so we could focus on just getting authorization
var obj = JObject.Parse(response.Content);
我们的服务器响应以下尝试POST
b'{"detail":"Authentication credentials were not provided."}
当我们查看服务器日志时,我们看到服务器正在将POST尝试作为GET读取,这可能更有趣
January 29th 2018, 14:23:39.633API request with method: GET, URI: /v1/user-tasks/ b'' b'{"detail":"Authentication credentials were not provided."}' GET