使用OAuth 1向Yahoo Weather API发送请求

16
我在使用Yahoo Weather API时遇到了问题,因为它没有给我任何数据。在访问YDN网站后,我发现从3月15日开始,所有请求都应该更新为OAuth 1(但我今天才使其工作!)。还说要包括Yahoo应用程序密钥和秘密。当我必须使用我的应用程序密钥和秘密时,请求URL应该是什么样子?
之前,我得到这样的请求字符串:https://query.yahooapis.com/v1/public/yql?q=SOME_QUERY&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback= 更新 在我最初提出这个问题的13分钟后,带有/v1/public/端点的API调用又可以工作了。但我仍然很想知道我的问题的答案。 更新 它又挂了 :(

2
昨天我们的公共访问出了问题,然后又恢复了,但昨晚或今天早上又再次出现了故障。雅虎的开发人员这周看起来并不好过。 - Kyle Challis
1
雅虎,你曾经很酷。 - energee
4个回答

12

1
可以用,你在哪里找到那个URL的? - Alejandro Fiore
1
不知道这是不是只是一个临时解决方案?现在看起来很好,但它可能会在不久的将来停止工作。 - Sujay Phadke

5

2016年4月中旬的当前解决方案 - 由于开发者的愤怒,雅虎再次允许无需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解决方法

您需要创建一个雅虎账户,然后在https://developer.yahoo.com/apps/create/上创建一个Web应用程序。

然后,您需要使用OAuth库来正确编码您的客户端ID和客户端密钥。以下是JavaScript示例,基于Yahoo Example Page2008 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部分。当这种情况发生时,我会尝试编辑此答案。根据Yahoo的说法,URL将保持不变,只是没有/public部分。最大的区别将是您需要发送请求标头与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">&deg;</span><sup>'+weather.units.temperature+'</sup></span><br><span class="wind-chill">Feels like: '+weather.wind.chill+'<span class="degree">&deg;</span></span></div></a>';
          $("#weather").html(html);
        });
    },
});

以上代码片段将把XML转换为JSON对象。在以下行中使用weather变量:var weather = data.query.results.rss.channel;不幸的是,雅虎的开发应用程序部分仍然无法使用。 - mattferderer
是的,我明白了,但这不适用于我的情况,因为我的应用程序是用Swift编写的(iOS应用程序)。我的意思是,是否有像您上面提供的那样的直接链接(http://weather.yahooapis.com/forecastrss?w=WOEID_GOES_HERE),可以获得JSON输出而不是XML? - phbelov
forecastrss目前返回:请提供有效的凭据。OAuth oauth_problem="OST_OAUTH_PARAMETER_ABSENT_ERROR",realm="yahooapis.com" - energee
在OAuth身份验证后,'/v1/yql'对任何人都有效吗?我得到了401错误。 - energee
您在 Plunkr 中使用的 OAUTH 参考链接无法正常工作,可以请您帮忙吗?谢谢。 - user318197
显示剩余6条评论

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);

我从这个讨论中找到了这个内容:(如何开始使用YQL的OAuth来获取历史股票数据?)


4
这是很棒的内容,但你忘记了打电话。我知道会有更多像我一样绝望和失落的人。 $results = $twoleg->query($query); - Hernan

2

显然,它不再使用公共API工作。从现在开始,您需要通过秘密密钥使用OAuth


9
好的,请问您能否提供一个OAuth 1的示例请求URL?我在文档中搜索了示例请求,但没有找到任何内容。 - phbelov
2
@phbelov 这是因为Yahoo的无用人员没有在任何地方记录它! - Sujay Phadke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接