【问题标题】:Yahoo Weather API using Oauth使用 Oauth 的 Yahoo Weather API
【发布时间】:2016-03-26 12:45:38
【问题描述】:

我一直在执行 Yahoo Weather 私有 API 调用。这是我的代码 sn-p,每当我使用有效的 clientId 和 Secret 调用它时,它都会返回 401(未经授权)。

var outhWc = new WebClient();
outhWc.Credentials = new NetworkCredential(clientId, clientSecret);
outhWc.Headers.Add(HttpRequestHeader.Accept, "application/json");
var outhresponse = outhWc.DownloadData("https://query.yahooapis.com/v1/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json");

它总是抛出异常。我也尝试在 NetworkCredentials 中传递用户名和密码,并尝试在 Header 中传递 clientId 和 Secret,但我找不到成功的调用。

【问题讨论】:

    标签: c# oauth yahoo-api


    【解决方案1】:

    所以我在这里遇到了同样的问题。最后我实现了以下代码,基于在http://oauth.googlecode.com/svn/code/csharp/OAuthBase.cs 找到的 oAuth c# 简单类

       public void LoadWeather() {
            string URLDes, Params = "";
            string Signature, BaseURL, cKey, cSecret = "";
    
            OAuthBase oAuth = new OAuthBase();
            BaseURL = "http://weather.yahooapis.com/forecastrss?w=" + textBox1.Text + "&u=f";
            cKey = "YOUR API KEY";
            cSecret = "YOUR API SECRET";
    
            Signature = oAuth.GenerateSignature(new Uri(BaseURL), cKey, cSecret, "", "", "GET", oAuth.GenerateTimeStamp(), oAuth.GenerateNonce(), out URLDes, out Params);
    
            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += HttpsCompleted;
            wc.DownloadStringAsync(new Uri(String.Format("{0}?{1}&oauth_signature={2}", URLDes, Params, Signature)));
        }
    
        private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e) {
            if (e.Error == null) {
                XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
                xdoc.Save("c:\\weather.xml");
                richTextBox1.Text = xdoc.FirstNode.ToString();
            } else {
                richTextBox1.Text = e.Error.Message;
            }
        }
    

    如您所见,我已经有了城市 ID。此示例下载 API 返回的 xml 字符串。为我工作,希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 2020-05-03
      • 2018-02-04
      • 2012-06-08
      • 1970-01-01
      相关资源
      最近更新 更多