cURL和.Net(c#)API令牌 [英] cURL and .Net (c#) API Token

查看:212
本文介绍了cURL和.Net(c#)API令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般已经阅读并重新阅读了stackoverflow和google搜索,但似乎找不到解决我问题的方法。我很肯定这是我的无知。

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.

我正在尝试在.Net(特别是C#)中重现一个cUrl调用,并且正在经历一段时光的魔鬼弄清楚为什么它不起作用。此cUrl调用使用令牌,而在使用令牌时不需要用户名。

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.

以下cUrl调用在命令行中按预期方式工作。 API令牌已更改,以保护无辜者。

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

我尝试了以下操作:

           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!");
        }

响应给出了错误请求。实际的requestmessage是:

The response gives a "Bad Request". The actual requestmessage is:


  • response.RequestMessage {Method: GET, RequestUri: 'https://deskapi.gotoassist.com/v1/incidents.json', Version: 1.1, Content: , Headers: { Accept: application/json }}

如果有帮助的话。

根据其他stackoverflow示例,我也尝试过:

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 {方法:GET,RequestUri:' https://deskapi.gotoassist.com/v1/incidents.json ',版本:1.1,内容:,标头:
    {
    接受:application / json
    授权:8cb60a319c71be3356da2ea6d7c7650b
    }} System.Net.Http.HttpRequestMessage

  • 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

实际的uri对我来说是正确的。内容类型在我看来是正确的。除了我尝试过的方法外,我找不到唯一的确定示例,就是发送 x:8cb60a319c71be3356da2ea6d7c7650b。

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.

推荐答案

cUrl命令使用 Content-Type 标头,仅用于发送正文时,例如使用PUT或POST。它与 Accept 的标头不同。 接受是通过GET请求发送的正确标头。

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.

当我尝试在没有任何身份验证标头的情况下访问服务器时,则返回400,并显示消息未知的OAuth签名方法。缺少身份验证的响应实际上应该是401和一个www-authenticate标头,告诉我们使用哪种方案。

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.

假定服务器实际上正在寻找一个OAuth2承载令牌,那么您可能想尝试一下,

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");

这篇关于cURL和.Net(c#)API令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆