【问题标题】:Yahoo Weather API call using Oauth C# or VB.net使用 Oauth C# 或 VB.net 调用 Yahoo Weather API
【发布时间】:2016-03-29 15:15:08
【问题描述】:

我无法通过以下代码使用 Yahoo 天气服务 API 进行身份验证。 我究竟做错了什么?我收到 401 - 未经授权。通过 xml.weather.yahoo.com 尝试过 - 同样的事情。我已经为此烦恼了一段时间,因此我们将不胜感激。

代码:

    Public Function getData() As String
    Dim resp As String = ""
    Try
        Dim consumerKey As String = "MY PRIVATE API KEY STRING GOES HERE"
        Dim consumerSecret As String = "SECRET WAS PLACED HERE"
        Dim uri = New Uri("https://query.yahooapis.com/v1/yql?q=SELECT%20*%20FROM%20weather.bylocation%20WHERE%20location%3D%27Kefar-Weradim%27%20AND%20unit%3D%22c%22&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
        Dim url As String, param As String
        Dim oAuth = New OAuthBase()
        Dim nonce = oAuth.GenerateNonce()
        Dim timeStamp = oAuth.GenerateTimeStamp()
        Dim signature = oAuth.GenerateSignature(uri, consumerKey, consumerSecret, String.Empty, String.Empty, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, url, param)
        Using _webResponse As WebResponse = WebRequest.Create(String.Format("{0}?{1}&oauth_signature={2}", url, param, signature)).GetResponse()
            Using reader As StreamReader = New StreamReader(_webResponse.GetResponseStream())
                resp = reader.ReadToEnd()
            End Using
        End Using
    Catch ex As Exception
        resp = "Error: " & ex.Message
    End Try
    Return resp
End Function

【问题讨论】:

  • 我看到来自 yahoo api 网站的奇怪行为。使用类似的代码,我的代码返回 401 - 在约 80% 的情况下未经授权。在大约 10% 的情况下,它会为我提供数据。 (其余 10% 用于其他随机故障)

标签: c# vb.net api oauth yahoo


【解决方案1】:

好的,所以我自己解决了这个问题。 解决方案有点不同,不需要身份验证。在雅虎更改了他们的身份验证策略后,我和许多其他人试图做的是让“按位置划分的天气”工作。但是,“天气预报”不需要这个。因此,这是上述问题的解决方法。

     Private Function GetDSfromYH() As DataSet
    Dim surl As String = ""
    Dim ds As New DataSet
    Dim sbResult As New StringBuilder
    Try
        'surl = "https://query.yahooapis.com/public/v1/yql?q=SELECT%20*%20FROM%20weather.bylocation%20WHERE%20location%3D%27Kefar-Weradim%27%20AND%20unit%3D%22c%22&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"'
        surl = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D1967578%20and%20u%3D%27c%27&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
        ds.ReadXml(surl)
        Return ds

    Catch ex As Exception
        sbResult.Append("<div>Exception in function 'GetDSfromYH': " & ex.Message & " </div>")
        sbResult.Append("<div style='max-width: 200px;'>Oops, there is a problem with the service. Please refresh the page or try later.</div>")
        divResults.Controls.Add(New LiteralControl(sbResult.ToString()))
    End Try


End Function

如您所见,我将旧的 URL 字符串注释掉以供参考。 新的 URL 字符串包含不同的方法,调用 weather.forecast 方法并使用 WOID。 你可以找到你的here。 重要提示:发送前无需使用任何方法对 url 进行编码。您可以“按原样”使用它。 我的函数中使用的 URL 在 %20u%3D%27c%27 部分包含摄氏度,其中“u”为单位,“c”为摄氏度。如果需要,您可以将“c”替换为“f”。 该函数返回一个包含预测所有部分的 DataSet - 每个部分都是它自己的数据表,您可以根据需要使用它。 祝朋友好运。

【讨论】:

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