之前,我得到这样的请求字符串:
https://query.yahooapis.com/v1/public/yql?q=SOME_QUERY&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
更新
在我最初提出这个问题的13分钟后,带有/v1/public/端点的API调用又可以工作了。但我仍然很想知道我的问题的答案。
更新
它又挂了 :(https://query.yahooapis.com/v1/public/yql?q=SOME_QUERY&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
更新
在我最初提出这个问题的13分钟后,带有/v1/public/端点的API调用又可以工作了。但我仍然很想知道我的问题的答案。
更新
它又挂了 :(您可以再次像以下这样编写不需要任何身份验证的查询:
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和客户端密钥。以下是JavaScript示例,基于Yahoo Example Page和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);
不幸的是,目前服务器已经关闭以创建该应用程序。理想情况下,一旦它们恢复正常,您就可以使用服务器端代码来完成oauth部分。当这种情况发生时,我会尝试编辑此答案。根据Yahoo的说法,URL将保持不变,只是没有/public部分。最大的区别将是您需要发送请求标头与URL一起验证您的帐户。
在那之前,这里有一个临时解决方案。您仍然可以使用带有邮政编码的YQL查询geo.places来获取woeid。
select * from geo.places where text=90210 limit 1
http://weather.yahooapis.com/forecastrss?w=WOEID_GOES_HERE
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);
});
},
});
我们最终使用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);
我从这个讨论中找到了这个内容:(如何开始使用YQL的OAuth来获取历史股票数据?)
$results = $twoleg->query($query);
- Hernan显然,它不再使用公共API工作。从现在开始,您需要通过秘密密钥
使用OAuth。