从c#调用时,Yelp Fusion API v3返回401 Unauthorized with TOKEN_MISSING错误

问题描述 投票:0回答:1

我在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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.