gpt4 book ai didi

c# - 如何使用 C# 访问 Yelp Fusion Api 的 OAuth2 token

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:57 26 4
gpt4 key购买 nike

我正在尝试使用 C# 获取 Yelp Fusion Api 的访问 OAuth2 token ,如文档中所述:https://www.yelp.com/developers/documentation/v3/get_started

但是,我收到错误:

未找到 client_id 或 client_secret。确保在应用程序正文中提供 client_id 和 client_secret application/x-www-form-urlencoded

以下是代码片段:

<code>
string baseURL = "https://api.yelp.com/oauth2/token";
Dictionary<string, string> query = new Dictionary<string, string>();

query["grant_type"] = "client_credentials";
query["client_id"] = CONSUMER_KEY;
query["client_secret"] = CONSUMER_SECRET;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseURL);
request.Accept = "application/json";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
requestWriter.Write(query.ToString());
requestWriter.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
Console.WriteLine(stream.ReadToEnd());
</code>

最佳答案

根据 Yelp Fusion 文档 here您需要进行 POST 调用,参数应以 application/x-www-form-urlencoded 格式发送。所以上面使用的方法是不正确的。

这应该有帮助:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(baseURL);
webRequest.Method = "POST";
webRequest.AllowAutoRedirect = true;
webRequest.Timeout = 20 * 1000;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";

//write the data to post request
String postData = "client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&grant_type=client_credentials";
byte[] buffer = Encoding.Default.GetBytes(postData);
if (buffer != null)
{
webRequest.ContentLength = buffer.Length;
webRequest.GetRequestStream().Write(buffer, 0, buffer.Length);
}
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResponse = reader.ReadToEnd();

请注意,上面的示例将返回字符串格式的数据。要读取实际值,您必须序列化数据。

编辑:自 2018 年 3 月 1 日起,yelp fusion API 的身份验证过程发生了变化。这不再适用。

关于c# - 如何使用 C# 访问 Yelp Fusion Api 的 OAuth2 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43554575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com