2017-11-02 2 views
0

私はYahooの天気サービスから以下のHTTPSエンドポイントを消費しようとしています:春ブーツとHTTPS GETサービスを消費する方法

Yahoo Weather Service API

私は現在を取得するためのAPIに応じていくつかの特別なクエリをしていますパラメータ化された場所の天気。

@Service("weatherConditionService") 
public class WeatherConditionServiceImpl implements WeatherConditionService { 

    private static final String URL = "http://query.yahooapis.com/v1/public/yql"; 

    public WeatherCondition getCurrentWeatherConditionsFor(Location location) { 
     RestTemplate restTemplate = new RestTemplate(); 
     StringBuilder stringBuilder = new StringBuilder(); 
     stringBuilder.append(URL); 
     stringBuilder.append("?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22"); 
     // TODO: Validate YQL query injection 
     stringBuilder.append(location.getName()); 
     stringBuilder.append("%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"); 
     WeatherQuery weatherQuery = restTemplate.getForObject(stringBuilder.toString(), WeatherQuery.class); 
     // TODO: Test Json mapping response 
     Condition condition = weatherQuery.getQuery().getResults().getChannel().getItem().getCondition(); 
     return new WeatherCondition(condition.getDate(), Integer.parseInt(condition.getTemp()), condition.getText()); 
    } 

場所は、「ニューヨーク」や「マニラ」など、場所の文字列の説明である属性「名前」を提供するクラスです。

条件他のクラスは戻ってくるオブジェクトをマップします。実行時には

私は、次のHTTPレスポンスを得る:

org.springframework.web.client.HttpClientErrorException: 403 Forbidden 

だからこれは私が私が理解からリソースにアクセスする権限がありませんよ意味します。

私はちょうど&は、Webブラウザに貼り付けてコピーした場合、URLは素晴らしい作品:

Yahoo Weather Query

私は「400」(不正なリクエスト)を取得しておりませんので、マッピングは問題ではないと思われるが、 "403"(禁止されています)

RestTemplateオブジェクトの使用方法には何らかのエラーがあります。私は研究していますが、私は答えを見つけることができません。

答えて

0

最終的に答えが見つかりました。私は(ないURLの一部として)、異なるパラメータを渡すために必要なので、それは最終的に不正な要求ました。

答えはhereです。ここで私の特定のYahoo Weather API呼び出しのコードはStringを返します(私はまだマッピングを使うためにいくつかの作業をしなければなりません)。

private static final String URL = "http://query.yahooapis.com/v1/public/yql"; 

    public String callYahooWeatherApi() { 

     RestTemplate restTemplate = new RestTemplate(); 

     HttpHeaders headers = new HttpHeaders(); 
     headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); 

     UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(URL) 
       .queryParam("q", "select wind from weather.forecast where woeid=2460286") 
       .queryParam("format", "json"); 

     HttpEntity<?> entity = new HttpEntity<>(headers); 

     HttpEntity<String> response = restTemplate.exchange(
       builder.build().encode().toUri(), 
       HttpMethod.GET, 
       entity, 
       String.class); 

     return response.getBody(); 

    } 
1

ドキュメントには、apiキーが必要だと言われています。しかし、私はこのような呼び出し作るとき:それはなしで正常に動作

fetch('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') 
.then(resp=> resp.json()) 
.then((res)=>console.log(res.query.results)) 

https://repl.it/NeoM

を。おそらくあなたはあまりにも頻繁にAPIを打つために黒人化されている。

コードは正常です。

+0

感謝。私はjavascriptを使って、または直接Webブラウザにそれを打つことができます。 Javaの例はないので、私はAPIキーを必要としないと推測しました。それがそれかもしれません。私はJavaでそれを行う方法を調べる必要があります。 – sebadagostino

関連する問題