maven-example-en icon indicating copy to clipboard operation
maven-example-en copied to clipboard

Yahoo API doesn't work anymore without OAuth credentials

Open jhagege opened this issue 8 years ago • 10 comments

Hi guys,

Just tried running the 4.8 chapter along the instructions, because couldn't do so. I receive an error: Server returned HTTP response code: 401 for URL: http://weather.yahooapis.com/forecastrss?p=60202

Looks like they are forcing OAuth authentication..

Thanks for the help

jhagege avatar May 09 '16 08:05 jhagege

Yeah, I also noticed this.

ghost avatar May 09 '16 13:05 ghost

Any forks with solutions on this?

marianormuro avatar Dec 07 '16 00:12 marianormuro

+1

ghost avatar Feb 14 '17 15:02 ghost

In YahooRetriver.java replace

String url = "http://weather.yahooapis.com/forecastrss?p=" + zipcode;

with

String url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places%20where%20placetype%3D%27Zip%27%20AND%20text%3D%27" + zipcode + "%27%20)";

Also in YahooParser in every doc.ValueOf() method replace /rss with /query/results like this:

weather.setCity(doc.valueOf("/query/results/channel/y:location/@city"));

obyknovenius avatar Feb 15 '17 08:02 obyknovenius

@obyknovenius Thank you very much for your url-update! I would be glad if you could provide a working example, because my manually performed request do not work... :(

Kind regards

ghost avatar Feb 20 '17 13:02 ghost

@Trzaska Sorry, I'm to lazy to create pull request :) Here is full text of my WeatherRetriever.java:

package com.obyknovenius.mavenbook.weather;

import org.apache.log4j.Logger;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class YahooRetriever {

    private static Logger log = Logger.getLogger(YahooRetriever.class);

    public InputStream retrieve(String zipcode) throws Exception {
        log.info("Retrieving Weather Data");
        String url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places%20where%20placetype%3D%27Zip%27%20AND%20text%3D%27" + zipcode + "%27%20)";
        URLConnection conn = new URL(url).openConnection();
        return conn.getInputStream();
    }
}

and YahooParser.java

package com.obyknovenius.mavenbook.weather;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class YahooParser {

    private static Logger log = Logger.getLogger(YahooParser.class);

    public Weather parse(InputStream inputStream) throws Exception {
        Weather weather = new Weather();

        log.info("Creating XML Reader");
        SAXReader xmlReader = createXmlReader();
        Document doc = xmlReader.read(inputStream);

        log.info("Parsing XML Response");
        weather.setCity(doc.valueOf("/query/results/channel/y:location/@city"));
        weather.setRegion(doc.valueOf("/query/results/channel/y:location/@region"));
        weather.setCountry(doc.valueOf("/query/results/channel/y:location/@country"));
        weather.setCondition(doc.valueOf("/query/results/channel/item/y:condition/@text"));
        weather.setTemp(doc.valueOf("/query/results/channel/item/y:condition/@temp"));
        weather.setChill(doc.valueOf("/query/results/channel/y:wind/@chill"));
        weather.setHumidity(doc.valueOf("/query/results/channel/y:atmosphere/@humidity"));

        return weather;
    }

    private SAXReader createXmlReader() {
        Map<String, String> uris = new HashMap<String, String>();
        uris.put("y", "http://xml.weather.yahoo.com/ns/rss/1.0");

        DocumentFactory factory = new DocumentFactory();
        factory.setXPathNamespaceURIs(uris);

        SAXReader xmlReader = new SAXReader();
        xmlReader.setDocumentFactory(factory);

        return xmlReader;
    }
}

obyknovenius avatar Feb 20 '17 13:02 obyknovenius

@obyknovenius No Problem! Know everything works like a charm! Thank you very much :) You helped me and certainly all the others a lot!

Kind regards!

ghost avatar Feb 20 '17 13:02 ghost

@Trzaska You can also replace this ugly line

String url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places%20where%20placetype%3D%27Zip%27%20AND%20text%3D%27" + zipcode + "%27%20)";

with this

String query = "select * from weather.forecast where woeid in (select woeid from geo.places where placetype='Zip' AND text='" + zipcode + "' )";
String url = "https://query.yahooapis.com/v1/public/yql?q=" + URLEncoder.encode(query, "UTF-8");

It's more self-explained. By the way, I've got this solution from here: http://stackoverflow.com/questions/36242098/simple-yahoo-weather-api-not-working And don't forget to update your test data in "ny-weather.xml". If you want, I also can put it here.

obyknovenius avatar Feb 20 '17 13:02 obyknovenius

did anybody have persistent issues with ssl with yahoo ? 0 INFO YahooRetriever - Retrieving Weather Data Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: access_denied

ldelaprade avatar Dec 07 '17 13:12 ldelaprade

Thank you @obyknovenius for sharing the updated code on weather API.

NIRAJGOEL avatar Apr 18 '18 13:04 NIRAJGOEL