I've read and re-read stackoverflow and google searches in general and I just can't seem to find a solution to my issue. I'm positive it is my ignorance.
I am trying to reproduce a cUrl call in .Net (c# specifically) and am having a devil of a time wading through and figuring out why it isn't working. this cUrl call uses a token and the username is not required when I do so.
The following cUrl call works as expected when called from the command line. The API token has been changed to protect the innocent.
curl -u x:8cb60a319c71be3356da2ea6d7c7650b -H "Content-Type: application/json" https://deskapi.gotoassist.com/v1/incidents.json
I have tried the following:
WebRequestHandler webHandler = new WebRequestHandler();
webHandler.Credentials = new System.Net.NetworkCredential("x", "8cb60a319c71be3356da2ea6d7c7650b");
HttpClient client = new HttpClient(webHandler);
client.BaseAddress = new Uri("https://deskapi.gotoassist.com/v1/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("incidents.json");
if (response.IsSuccessStatusCode)
{
Console.Write("Success!");
}
The response gives a "Bad Request". The actual requestmessage is:
If that helps any.
Based on other stackoverflow examples I also tried:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://deskapi.gotoassist.com/v1/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("8cb60a319c71be3356da2ea6d7c7650b");
HttpResponseMessage response = await client.GetAsync("incidents.json");
if (response.IsSuccessStatusCode)
{
Console.Write("Success!");
}
Console.Write(response.RequestMessage.ToString());
With another "Bad Request". The message is similar:
- response.RequestMessage {Method: GET, RequestUri: 'https://deskapi.gotoassist.com/v1/incidents.json', Version: 1.1, Content: , Headers:
{
Accept: application/json
Authorization: 8cb60a319c71be3356da2ea6d7c7650b
}} System.Net.Http.HttpRequestMessage
The actual uri looks correct to me. The content type looks correct to me. The only thing I can't find a definitive example to do is send that "x:8cb60a319c71be3356da2ea6d7c7650b" other than the ways I have tried it.
Any thoughts? This is my first time trying to do anything like this so hopefully I'm just doing some ignorant something-something. I've tried to follow example after example but nothing gets me an successful call. Thank you in advance for helping.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…