c# - 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
}}
解决方案
当尝试通过 httpclient 或 restsharp 执行相同的 GET 时,我得到了 401 未授权。
WWW-Authenticate: Basic Realm="Shopify API 身份验证"
HTTP 401 Unauthorized 错误表示该请求尚未应用,因为它缺少目标资源的有效身份验证凭据。
请检查您是否在Authorization
标题字段中提供了身份验证凭据。
Authorization: Basic Your_base64EncodedAuthenticationString_Here
推荐阅读
- sql - 我缺少什么加入条件
- oracle - Toad:自动在两个 Oracle 表之间进行每日传输?
- javascript - 为什么 useEffect 不再运行我的 API 提取并将结果保存到状态?
- zend-framework - LUCENE FULLTEXT INDEX (Orientdb / Lucene) 上的十进制范围搜索查询
- javascript - 在 NodeJS 中过滤和排序文档数组
- sql - 提取 XML 数据并输出到文件以保存在文件夹中
- sql - WHEN 语句中具有多个 LIKE 和 NOT LIKE 的 SQL 查询
- c# - 无法使用c#运行命令
- html - 打印时如何在 React 中操作主题?
- c++ - 如何实现电压降的计算?