strXiaoCaiNiao 2020-10-15 18:01 采纳率: 0%
浏览 219

如何用WebClient调用带有Bearer Token的REST API? 谢谢!

根据https://www.mysalemarketplace.com/docs/#orders中API的文档:

curl "/v1/orders/new/"
-H "Authorization: Bearer XyZAlexYustasu::NeSHarit00Etot42klyuch"

想用WebClient去获取新的订单:

            using (WebClient webClient = new WebClient())
            {
                string path = @"https://cdn.mysalemarketplace.com/v1/orders/new/";

                string token = "XyZAlexYustasu::NeSHarit00Etot42klyuch";
                webClient.Headers.Add(HttpRequestHeader.Authorization, string.Format("Bearer {0}", token));

                byte[] responseData = webClient.DownloadData(path);
                string result = Encoding.UTF8.GetString(responseData);

                return result;
            }

结果返回403 Forbidden,提示“没有权限访问”,
换成正式的token

wclient.Headers.Add("Authorization", "Bearer eyJhbGciOiJIU.........8Kn56fDXWh-lA");

也是一样的错误。

请问:如何用WebClient调用带有Bearer Token的REST API? 谢谢!
(也想用HttpClient试试)

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-10-16 12:46
    关注
    评论

报告相同问题?