首页 > 解决方案 > GET 请求在浏览器和 Postman 中有效,但在使用 HttpClient 和 RestSharp 时我得到了未经授权

问题描述

我正在通过 Postman 发出请求

“https:// APIKEY密码@ myexampledomain.myshopify.com / api/2021-04/custom_collections.json

我得到了真实的回应:

{
    "custom_collections": [
        {
            "id": 264277491901,
            "handle": "baran-kategori",
            "updated_at": "2021-04-08T18:05:03+03:00",
            "published_at": "2021-04-05T16:40:08+03:00",
            "sort_order": "best-selling",
            "template_suffix": "",
            "published_scope": "web",
            "title": "Baran Kategori",
            "body_html": ""
            
        },
        {
            "id": 264276377789,
            "handle": "category1",
            "updated_at": "2021-04-06T14:49:26+03:00",
            "published_at": "2021-04-05T16:12:36+03:00",
            "sort_order": "best-selling",
            "template_suffix": "",
            "published_scope": "web",
            "title": "Category1",
            "body_html": ""
            
        },
        {
            "id": 264362623165,
            "handle": "nike",
            "updated_at": "2021-04-08T18:05:03+03:00",
            "published_at": "2021-04-06T16:17:49+03:00",
            "sort_order": "best-selling",
            "template_suffix": "",
            "published_scope": "web",
            "title": "Nike",
            "body_html": ""
            
        }
    ]
}

当尝试通过 httpclient 或 restsharp 执行相同的 GET 时,我得到了 401 未授权。

{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Date: Thu, 08 Apr 2021 16:12:33 GMT
  Transfer-Encoding: chunked
  Connection: keep-alive
  X-Sorting-Hat-PodId: 188
  X-Sorting-Hat-ShopId: 56072536253
  Referrer-Policy: origin-when-cross-origin
  X-Frame-Options: DENY
  X-ShopId: 56072536253
  X-ShardId: 188
  WWW-Authenticate: Basic Realm="Shopify API Authentication"
   Content-Type: application/json; charset=utf-8
  Content-Length: 89
}}

标签: c#apiasp.net-core

解决方案


当尝试通过 httpclient 或 restsharp 执行相同的 GET 时,我得到了 401 未授权。

WWW-Authenticate: Basic Realm="Shopify API 身份验证"

HTTP 401 Unauthorized 错误表示该请求尚未应用,因为它缺少目标资源的有效身份验证凭据。

请检查您是否在Authorization标题字段中提供了身份验证凭据。

Authorization: Basic Your_base64EncodedAuthenticationString_Here

推荐阅读