简体   繁体   English

将CURL请求转换为C#以访问Snipcart API会返回401未经授权

[英]Converting CURL request to C# for accessing Snipcart API returns 401 Unauthorized

I am trying to access the Snipcart API (Reference - https://docs.snipcart.com/api-reference/authentication ). 我试图访问Snipcart API(参见- https://docs.snipcart.com/api-reference/authentication )。 I have created an API key and followed the steps mentioned in the documentation. 我创建了一个API密钥,并按照文档中提到的步骤进行操作。

I am trying to Convert a CURL request to C#. 我正在尝试将CURL请求转换为C#。 I chose to use HttpWebRequest. 我选择使用HttpWebRequest。 I get 401 Unauthorized returned from the Server. 我从服务器返回了401未经授权。 I am not able to figure out what's wrong with my request. 我无法弄清楚我的要求出了什么问题。

Actual Curl Request: - curl -H "Accept: application/json" https://app.snipcart.com/api/orders -u {API_KEY}: 实际的卷曲请求:-curl -H“接受:application / json” https://app.snipcart.com/api/orders -u {API_KEY}:

The following is the code that i tried converting the above curl request to 以下是我尝试将上述curl请求转换为的代码

        string baseURL = "https://app.snipcart.com/api/orders";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseURL);
        req.AllowAutoRedirect = true;
        req.ContentType = "application/json";
        req.Accept = "application/json";
        req.Method = "GET";
        req.Headers.Add("API_Key", "MY_API_KEY_VALUE_COMES_HERE");

        string jsonResponse = null;
        using (WebResponse resp = req.GetResponse())
        {
            if (req.HaveResponse && resp != null)
            {
                using (var reader = new StreamReader(resp.GetResponseStream()))
                {
                    jsonResponse = reader.ReadToEnd();
                }
            }
        }
        Console.Write(jsonResponse);

The API reference from Snipcart says you need Basic HTTP Authentication . 来自Snipcart的API参考指出您需要基本的HTTP身份验证

When I have a look at your code, I think you should change this line 当我查看您的代码时,我认为您应该更改此行

req.Headers.Add("API_Key", "MY_API_KEY_VALUE_COMES_HERE");

to

req.Credentials = new NetworkCredential("API_Key", "MY_API_KEY_VALUE_COMES_HERE");

A kind of the same issue is described in this topic , you can take it as reference. 主题描述了一种相同的问题,您可以将其作为参考。

If it's not solving the issue, you could have a closer look at the curl API parameters manual , and then translate it to C# code. 如果仍不能解决问题,则可以仔细阅读curl API参数手册 ,然后将其转换为C#代码。

curl -H "Accept: application/json" \
  https://app.snipcart.com/api/orders \
  -u {API_KEY}:

You need to send the header as a basic auth header instead of "API_Key" Try something like this. 您需要将标头作为基本身份验证标头发送,而不是“ API_Key”发送。尝试类似这样的操作。

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseURL);
req.AllowAutoRedirect = true;
req.ContentType = "application/json";
req.Accept = "application/json";
req.Method = "GET";
var basicAuthHeader = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("MY_API_KEY_VALUE_COMES_HERE"));
req.Headers.Add("Authorization", "Basic " + basicAuthHeader);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM