【问题标题】:Yelp Fusion API v3 returns 401 Unauthorized with TOKEN_MISSING error when called from c#从 c# 调用时,Yelp Fusion API v3 返回 401 Unauthorized with TOKEN_MISSING 错误
【发布时间】:2018-09-16 17:05:47
【问题描述】:

我在 Yelp 中创建了我的应用程序,获得了我的 api 密钥,并且在执行业务搜索时 Postman 一切正常。

但是,从 c# 进行测试时,我收到一个 401 未经授权的错误,其中包含一个 TOKEN_MISSING 错误,上面写着 ""{\"error\": {\"code\": \"TOKEN_MISSING\", \"description\": \"必须提供访问令牌才能使用此端点。\"}}""。

不过,我正确地提供了我的 api 密钥,而 Yelp 文档说这就是我所需要的,所以我不确定问题出在哪里。以下是 2 个不起作用的单独 c# 代码示例(出于安全考虑,我已将实际的 api 密钥替换为):

使用 WebRequest 的示例:

var webRequest = WebRequest.Create("http://api.yelp.com/v3/businesses/search?term=Clayton+Bicycle+Center&location=5411+Clayton+Rd%2c+Clayton%2c+CA+94517%2c+US");
webRequest.Method = "GET";
webRequest.Headers.Add("Cache-Control", "no-cache");
webRequest.Headers.Add("Authorization", "Bearer <my_api_key>");
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
var stream = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
var content = stream.ReadToEnd();
Console.Write(content);

使用 RestSharp 的示例:

var client = new RestClient("http://api.yelp.com/v3/businesses/search?term=Clayton+Bicycle+Center&location=5411+Clayton+Rd%2c+Clayton%2c+CA+94517%2c+US");
var request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "Bearer <my_api_key>");
var response = client.Execute(request);
Console.Write(response.Content);

我已经检查了 Fiddler 中的请求,并且两者都发送与正在工作的 Postman 搜索相同的标头,但在 Postman 返回搜索结果时,两者都返回 401 未经授权的错误。有什么想法吗?

编辑:

这很尴尬,显然我的问题是我试图通过 http 而不是 https 访问 Yelp API。一旦我更改为 https,一切都按预期工作。

【问题讨论】:

    标签: c# api restsharp yelp unauthorized


    【解决方案1】:

    将端点更改为使用 https 而不是 http,现在可以使用。

    【讨论】:

      猜你喜欢
      • 2016-12-17
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 2021-08-03
      相关资源
      最近更新 更多