我无法通过以下代码通过Yahoo weather service API进行身份验证。 我究竟做错了什么?我得到401 - 未经授权。通过xml.weather.yahoo.com尝试 - 同样的事情。
我现在已经有一段时间不知所措了。所以任何帮助都会受到赞赏。CODE:
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
答案 0 :(得分:0)
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进行编码。您可以使用它&#34; As-Is&#34;。 我的函数中使用的URL包含%20u%3D%27c%27部分的摄氏度,其中包含&#34; u&#34;作为单位和&#34; c&#34;作为摄氏温度。你可以替换&#34; c&#34;用&#34; f&#34;如果需要的话。 该函数返回一个DataSet,其中包含预测的所有部分 - 每个部分都是它自己的数据表,您可以根据需要使用它。 祝你好运。