Last active
August 26, 2019 12:02
-
-
Save VerizonMediaOwner/6b4727a988b858362195d62a4c726fd9 to your computer and use it in GitHub Desktop.
Yahoo Weather API Java Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms. | |
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.Base64; | |
import java.util.Base64.Encoder; | |
import java.util.Random; | |
import java.util.Collections; | |
import java.net.URLEncoder; | |
import java.net.http.HttpClient; | |
import java.net.http.HttpRequest; | |
import java.net.http.HttpResponse; | |
import java.net.http.HttpResponse.BodyHandlers; | |
import java.net.URI; | |
/** | |
* | |
* <pre> | |
* % java --version | |
* % java 11.0.1 2018-10-16 LTS | |
* | |
* % javac WeatherYdnJava.java && java -ea WeatherYdnJava | |
* </pre> | |
* | |
*/ | |
public class WeatherYdnJava { | |
public static void main(String[] args) throws Exception { | |
final String appId = "test-app-id"; | |
final String consumerKey = "your-consumer-key"; | |
final String consumerSecret = "your-consumer-secret"; | |
final String url = "https://weather-ydn-yql.media.yahoo.com/forecastrss"; | |
long timestamp = new Date().getTime() / 1000; | |
byte[] nonce = new byte[32]; | |
Random rand = new Random(); | |
rand.nextBytes(nonce); | |
String oauthNonce = new String(nonce).replaceAll("\\W", ""); | |
List<String> parameters = new ArrayList<>(); | |
parameters.add("oauth_consumer_key=" + consumerKey); | |
parameters.add("oauth_nonce=" + oauthNonce); | |
parameters.add("oauth_signature_method=HMAC-SHA1"); | |
parameters.add("oauth_timestamp=" + timestamp); | |
parameters.add("oauth_version=1.0"); | |
// Make sure value is encoded | |
parameters.add("location=" + URLEncoder.encode("sunnyvale,ca", "UTF-8")); | |
parameters.add("format=json"); | |
Collections.sort(parameters); | |
StringBuffer parametersList = new StringBuffer(); | |
for (int i = 0; i < parameters.size(); i++) { | |
parametersList.append(((i > 0) ? "&" : "") + parameters.get(i)); | |
} | |
String signatureString = "GET&" + | |
URLEncoder.encode(url, "UTF-8") + "&" + | |
URLEncoder.encode(parametersList.toString(), "UTF-8"); | |
String signature = null; | |
try { | |
SecretKeySpec signingKey = new SecretKeySpec((consumerSecret + "&").getBytes(), "HmacSHA1"); | |
Mac mac = Mac.getInstance("HmacSHA1"); | |
mac.init(signingKey); | |
byte[] rawHMAC = mac.doFinal(signatureString.getBytes()); | |
Encoder encoder = Base64.getEncoder(); | |
signature = encoder.encodeToString(rawHMAC); | |
} catch (Exception e) { | |
System.err.println("Unable to append signature"); | |
System.exit(0); | |
} | |
String authorizationLine = "OAuth " + | |
"oauth_consumer_key=\"" + consumerKey + "\", " + | |
"oauth_nonce=\"" + oauthNonce + "\", " + | |
"oauth_timestamp=\"" + timestamp + "\", " + | |
"oauth_signature_method=\"HMAC-SHA1\", " + | |
"oauth_signature=\"" + signature + "\", " + | |
"oauth_version=\"1.0\""; | |
HttpClient client = HttpClient.newHttpClient(); | |
HttpRequest request = HttpRequest.newBuilder() | |
.uri(URI.create(url + "?location=sunnyvale,ca&format=json")) | |
.header("Authorization", authorizationLine) | |
.header("X-Yahoo-App-Id", appId) | |
.header("Content-Type", "application/json") | |
.build(); | |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); | |
System.out.println(response.body()); | |
} | |
} |
By default, 7 days of forecast results are returned。 How can I get 10 days of forest results,or more days?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i am doing this in android and its giving me a signature like this >> BEITE3zsd8/Zd5VAk8n6HGmB558=
but this signature gives me a 401. but a signature from php code is like - %2FvXKImSZj05k2p7hmwl63gO%2Fzns%3D. and this gives a good response. please suggest any solution.