phb*_*lov 16 url oauth request yql yahoo-api
我遇到了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=
UPDATE
我最初问这个问题后13分钟,/ v1/public/endpoint的API调用再次正常工作.但是对我来说,回答我的问题仍然很有趣.
UPDATE
又下来了:(
您可以再次编写查询,而无需进行任何身份验证,如下所示:
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
以下答案如果他们对任何人都有帮助.在雅虎改变的某些时期,他们确实有效.
您需要创建一个Yahoo帐户,然后在https://developer.yahoo.com/apps/create/创建一个Web应用程序.
然后,您需要使用OAuth库来正确编码您的客户端ID和客户端密钥.以下是一个基于JavaScript的示例,基于一个由Yahoo Donnelly撰写的Yahoo Example Page和一篇2008年博客文章.这将生成用于请求天气预报的编码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);
Run Code Online (Sandbox Code Playgroud)
应该注意的是,永远不要向公众展示您的消费者密钥或消费者秘密.您可以在此Plunkr中使用自己的Yahoo Credentials:http://plnkr.co/edit/EvLbgs
不幸的是,到目前为止,服务器已经关闭以创建该应用程序.理想情况下,一旦他们备份,您可以使用服务器端代码来执行oauth部分.当发生这种情况时,我会尝试编辑这个答案.根据雅虎,除了没有/ public部分之外,URL将是相同的.最大的区别在于您需要使用验证帐户的网址发送请求标头.
在此之前,这是一个临时修复.你仍然可以使用带有邮政编码的YQL查询geo.places来获取woeid.
select * from geo.places where text=90210 limit 1
Run Code Online (Sandbox Code Playgroud)
然后你可以从那里抓住你的woeid并在下面的url中使用它来获取xml feed:
http://weather.yahooapis.com/forecastrss?w=WOEID_GOES_HERE
Run Code Online (Sandbox Code Playgroud)
我在这里创建了一个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);
});
},
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23761 次 |
| 最近记录: |