我遇到了Yahoo Weather API的问题,因为它没有给我任何数据。访问YDN网站后,我发现所有请求都应该从3月15日开始更新到OAuth 1(但我今天才开始工作!)。它也被称为包括雅虎App密钥和秘密。当我必须使用我的app密钥和秘密时,请求网址应该是什么样的?
之前,我收到了这样的请求字符串:https://query.yahooapis.com/v1/public/yql?q=SOME_QUERY&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
更新
我最初问这个问题13分钟后,/ v1 / public / endpoint的API调用再次起作用。但是对我来说,回答我的问题仍然很有趣。
更新
再次下降:(
答案 0 :(得分:12)
答案 1 :(得分:5)
您可以再次编写查询而无需进行任何身份验证,如下所示:
https://query.yahooapis.com/v1/public/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&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
以下答案如果他们对任何人都有帮助。在雅虎改变的某些时期,他们确实有效。
您需要创建一个雅虎帐户&然后在https://developer.yahoo.com/apps/create/
创建一个Web应用程序然后,您需要使用OAuth库正确编码您的客户端ID&客户秘密。以下是基于Yahoo Example Page&的JavaScript的示例。一个2008 Blog Article by Paul Donnelly。这将生成用于请求天气预报的编码URL。
//Fill in your consumer Key & Secret from Yahoo's App & adjust location as needed.
//This Key & Secret combination is invalid & won't work for you
var consumerKey = "dj0yJmk9NkRjbXpjUEhPbjlnJmQ9WVdrOVFUQTFaV2wxTjJrbXnHbz3NQSktJnM9Y29uc3VtZXJzZWNyZXQmeD0wOQ--";
var consumerSecret = "9bea8a9k3934d16365ek7e23e0abo1bba4q5c03c";
var locationToQuery = "90210"; //Can be zip code or anything that works in the query select woeid from geo.places(1) where text=<Your Location>
var makeSignedRequest = function(ck,cs,loc) {
var encodedurl = "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%22"+loc+"%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
var accessor = { consumerSecret: cs, tokenSecret: ""};
var message = { action: encodedurl, method: "GET", parameters: [["oauth_version","1.0"],["oauth_consumer_key",ck]]};
OAuth.setTimestampAndNonce(message);
OAuth.SignatureMethod.sign(message, accessor);
var parameterMap = OAuth.getParameterMap(message);
var baseStr = OAuth.decodeForm(OAuth.SignatureMethod.getBaseString(message));
var theSig = "";
if (parameterMap.parameters) {
for (var item in parameterMap.parameters) {
for (var subitem in parameterMap.parameters[item]) {
if (parameterMap.parameters[item][subitem] == "oauth_signature") {
theSig = parameterMap.parameters[item][1];
break;
}
}
}
}
var paramList = baseStr[2][0].split("&");
paramList.push("oauth_signature="+ encodeURIComponent(theSig));
paramList.sort(function(a,b) {
if (a[0] < b[0]) return -1;
if (a[0] > b[0]) return 1;
if (a[1] < b[1]) return -1;
if (a[1] > b[1]) return 1;
return 0;
});
var locString = "";
for (var x in paramList) {
locString += paramList[x] + "&";
}
var finalStr = baseStr[1][0] + "?" + locString.slice(0,locString.length - 1);
return finalStr;
};
//Use the encodedURL to make your request
var encodedURL = makeSignedRequest(consumerKey, consumerSecret, locationToQuery);
应该注意的是,永远不要向公众展示您的消费者密钥或消费者秘密。您可以在此Plunkr中使用自己的Yahoo凭据:http://plnkr.co/edit/EvLbgs
不幸的是,截至目前,服务器已关闭以创建该应用。理想情况下,一旦他们重新启动,您就可以使用服务器端代码来执行oauth部分。当发生这种情况时,我会尝试编辑这个答案。根据雅虎,除了没有/ public部分之外,URL将是相同的。最大的区别在于您需要使用验证帐户的网址发送请求标头。
在此之前,这是一个临时修复。你仍然可以使用带有邮政编码的YQL查询geo.places来获取woeid。
select * from geo.places where text=90210 limit 1
然后你可以从那里抓住你的woeid&amp;在以下网址中使用它来获取xml Feed:
http://weather.yahooapis.com/forecastrss?w=WOEID_GOES_HERE
我在这里创建了一个Plunker作为此临时修复的示例:http://plnkr.co/edit/dClPDtnToMhHqvKpfCzj?p=preview
这里是使用jQuery的主旨:
var zipCode = 90210;
$.ajax({
dataType: "json",
headers: { "Accept": "application/json; odata=verbose" },
url: "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D"+zipCode+"%20limit%201&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys",
beforeSend: function(xhr){xhr.setRequestHeader('Accept', 'application/json; odata=verbose');},
success: function(data){
$.getJSON("https://query.yahooapis.com/v1/public/yql?callback=?", {
q: "select * from xml where url=\"https://weather.yahooapis.com/forecastrss?w="+data.query.results.place.locality1.woeid+"\"",
format: "json"
},function (data) {
var weather = data.query.results.rss.channel;
var html = '<div><span class="temperature">'+weather.item.condition.temp+'<span class="degree">°</span><sup>'+weather.units.temperature+'</sup></span><br><span class="wind-chill">Feels like: '+weather.wind.chill+'<span class="degree">°</span></span></div></a>';
$("#weather").html(html);
});
},
});
答案 2 :(得分:4)
我们终于使用yosdk使用双腿授权再次使用yql天气:(https://github.com/isaacs/authentipede)
并使用此脚本
<?php
include_once("yosdk/lib/Yahoo.inc");
define("API_KEY","your-api-key-here");
define("SHARED_SECRET","your-secret-here");
YahooLogger::setDebug(true);
$twoleg = new YahooApplication (API_KEY, SHARED_SECRET);
$query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="84054") and u="f"';
print_r ($results);
我从这次讨论中发现了这一点:(How do I get started with oauth for YQL for historical stock data?)
答案 3 :(得分:2)
显然,它意味着不再使用公共API 。目前,您需要使用 OAuth 到Secret Key
。