使用 OAuth 1 发出 Yahoo Weather API 请求
Making Yahoo Weather API request with OAuth 1
我 运行 遇到了 Yahoo Weather API 的问题,因为它没有给我任何数据。访问 YDN 网站后,我发现从 3 月 15 日开始,所有请求都应更新为 OAuth 1(但我今天才开始工作!)。据说还包括 Yahoo App 密钥和机密。当我必须使用我的应用程序密钥和秘密时,请求 url 现在应该是什么样子?
之前,我得到这样的请求字符串:https://query.yahooapis.com/v1/public/yql?q=SOME_QUERY&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
更新
在我最初问这个问题 13 分钟后 API 使用 /v1/public/ 端点的调用再次工作。但是得到问题的答案对我来说仍然很有趣。
更新
又跌了:(
显然,它意味着不再使用 Public API。从现在开始,您将需要使用 OAuth 到 Secret Key
.
截至 Mid-April 2016 年的当前解决方案 - 由于开发人员的愤怒,雅虎再次允许没有 Oauth 的 YQL 请求
您可以再次编写查询,无需任何身份验证,如下所示:
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
以下以前的答案,以防对任何人有帮助。在雅虎的某些变化时期,它们确实起作用了。
以下是由于历史原因可能仍然有效的此答案的旧版本
雅虎最新一轮更新后的更新答案 - 不安全的 OAuth 解决方法
您需要创建一个 Yahoo 帐户,然后在 https://developer.yahoo.com/apps/create/
创建一个 Web 应用程序
然后您将需要使用 OAuth 库来正确编码您的客户端 ID 和客户端密码。这是 JavaScript 中基于 Yahoo Example Page & a 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);
请注意,切勿向 public 显示您的用户密钥或用户机密。您可以在此 Plunkr 中使用您自己的 Yahoo Credentials:http://plnkr.co/edit/EvLbgs
原答案
不幸的是,截至目前,服务器无法创建该应用程序。理想情况下,一旦备份,您就可以使用服务器端代码来完成 oauth 部分。发生这种情况时,我会尝试编辑此答案。根据雅虎的说法,URL 将是相同的,除了没有 /public 部分。最大的区别是您需要发送请求 headers 和验证您帐户的 URL。
这是在此之前的临时修复。您仍然可以使用带有邮政编码的 YQL 查询 geo.places 来获取 woeid。
select * from geo.places where text=90210 limit 1
然后您可以从那里获取您的 woeid 并在以下 url 中使用它来获取 xml 提要:
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);
});
},
});
我们终于通过使用 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?)
如果你只是替换
和
它应该工作 ;)
我 运行 遇到了 Yahoo Weather API 的问题,因为它没有给我任何数据。访问 YDN 网站后,我发现从 3 月 15 日开始,所有请求都应更新为 OAuth 1(但我今天才开始工作!)。据说还包括 Yahoo App 密钥和机密。当我必须使用我的应用程序密钥和秘密时,请求 url 现在应该是什么样子?
之前,我得到这样的请求字符串:https://query.yahooapis.com/v1/public/yql?q=SOME_QUERY&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
更新
在我最初问这个问题 13 分钟后 API 使用 /v1/public/ 端点的调用再次工作。但是得到问题的答案对我来说仍然很有趣。
更新
又跌了:(
显然,它意味着不再使用 Public API。从现在开始,您将需要使用 OAuth 到 Secret Key
.
截至 Mid-April 2016 年的当前解决方案 - 由于开发人员的愤怒,雅虎再次允许没有 Oauth 的 YQL 请求
您可以再次编写查询,无需任何身份验证,如下所示:
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
以下以前的答案,以防对任何人有帮助。在雅虎的某些变化时期,它们确实起作用了。
以下是由于历史原因可能仍然有效的此答案的旧版本
雅虎最新一轮更新后的更新答案 - 不安全的 OAuth 解决方法
您需要创建一个 Yahoo 帐户,然后在 https://developer.yahoo.com/apps/create/
创建一个 Web 应用程序然后您将需要使用 OAuth 库来正确编码您的客户端 ID 和客户端密码。这是 JavaScript 中基于 Yahoo Example Page & a 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);
请注意,切勿向 public 显示您的用户密钥或用户机密。您可以在此 Plunkr 中使用您自己的 Yahoo Credentials:http://plnkr.co/edit/EvLbgs
原答案
不幸的是,截至目前,服务器无法创建该应用程序。理想情况下,一旦备份,您就可以使用服务器端代码来完成 oauth 部分。发生这种情况时,我会尝试编辑此答案。根据雅虎的说法,URL 将是相同的,除了没有 /public 部分。最大的区别是您需要发送请求 headers 和验证您帐户的 URL。
这是在此之前的临时修复。您仍然可以使用带有邮政编码的 YQL 查询 geo.places 来获取 woeid。
select * from geo.places where text=90210 limit 1
然后您可以从那里获取您的 woeid 并在以下 url 中使用它来获取 xml 提要:
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);
});
},
});
我们终于通过使用 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?)
如果你只是替换
和
它应该工作 ;)