Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

cURL and .Net (c#) API Token

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The cUrl command uses the Content-Type header which is only intended for when you are sending a body e.g. with PUT or POST. It is not the same header as Accept. Accept is the correct header to send with a GET request.

When I try hitting the server without any auth header, I get back a 400 with the message "Unknown OAuth signature method". The response for missing auth should really be a 401 and a www-authenticate header that tells us what kind of scheme to use.

Assuming that the server is actually looking for a OAuth2 bearer token, then you might want to try,

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer","8cb60a319c71be3356da2ea6d7c7650b");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...