免费的雅虎天气应用创建, free yahoo weather application create ,Homepage URL , java demo示例代码

国内网可能访问不了,可能需要梯子

官网

免费雅虎天气应用创建,获取授权key:
https://developer.yahoo.com/apps/create/

如果没账号,注册一个,简单用邮箱就可以注册了,我用的qq邮箱注册的。

创建应用

非常简单,如下图,我就输入了个 Application Name 和 Description 还有 Homepage Url 然后点击 Create App 立刻就创建好了
在这里插入图片描述
注意Homepage URLRedirect URL 填写你的浏览器打开后默认的首页地址即可,比如我的这里 填写的 https://hao.360.com/?a1004

如下图:
在这里插入图片描述

让人无语的是,我在国内外网上搜了n多的 yahoo weather Homepage URL 怎么填写的,硬是没有找到一个明确说怎么填的。所以我才写了这篇文章。

App ID, Client ID, Client Secret

点击 Create App 后,如果成功了立即就可以看到 App ID, Client ID, Client Secret 信息了,如下图:
在这里插入图片描述

雅虎天气api开发文档

yahoo weather API Documentation

https://developer.yahoo.com/weather/documentation.html

Java Demo

代码来源

https://blog.csdn.net/qq1594443513/article/details/108770757?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

对官方文档的demo做了一些小修改,因为官方文档使用的是jdk11,我使用的是jdk1.8,有些方法没有所以使用了org.apache.http.impl.client.CloseableHttpClient;模拟http请求。

简单的java demo源码 (能正常跑起来,可能需要梯子

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import java.util.*;
import java.util.Base64.Encoder;
import java.net.URLEncoder;
import java.net.URI;

public class Test {
    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\"";

        // 定义一个CloseableHttpClient
        CloseableHttpClient client = HttpClientBuilder.create().build();
        // 定义响应接收对象
        CloseableHttpResponse request;

        HttpGet httpGet = new HttpGet(URI.create(url + "?location=sunnyvale,ca&format=json"));
        httpGet.addHeader("Authorization", authorizationLine);
        httpGet.addHeader("X-Yahoo-App-Id", appId);
        httpGet.addHeader("Content-Type", "application/json");
        request=client.execute(httpGet);

        // 获取查询结果
        HttpEntity requestponseEntity = request.getEntity();

        System.out.println("Response content: " + EntityUtils.toString(requestponseEntity));
    }
}

上面代码 替换自己的 appid key 和 Secret ,运行后结果如下:

{"location":{"city":"Sunnyvale","region":" CA","woeid":2502265,"country":"United States","lat":37.371609,"long":-122.038254,"timezone_id":"America/Los_Angeles"},"current_observation":{"wind":{"chill":57,"direction":345,"speed":4.35},"atmosphere":{"humidity":53,"visibility":10.0,"pressure":30.06,"rising":0},"astronomy":{"sunrise":"7:16 am","sunset":"4:52 pm"},"condition":{"text":"Mostly Cloudy","code":28,"temperature":57},"pubDate":1608073200},"forecasts":[{"day":"Tue","date":1608019200,"low":42,"high":58,"text":"Partly Cloudy","code":30},{"day":"Wed","date":1608105600,"low":41,"high":59,"text":"Mostly Cloudy","code":28},{"day":"Thu","date":1608192000,"low":45,"high":56,"text":"Rain","code":12},{"day":"Fri","date":1608278400,"low":38,"high":57,"text":"Mostly Sunny","code":34},{"day":"Sat","date":1608364800,"low":40,"high":57,"text":"Mostly Sunny","code":34},{"day":"Sun","date":1608451200,"low":39,"high":57,"text":"Mostly Sunny","code":34},{"day":"Mon","date":1608537600,"low":42,"high":58,"text":"Mostly Sunny","code":34},{"day":"Tue","date":1608624000,"low":42,"high":58,"text":"Partly Cloudy","code":30},{"day":"Wed","date":1608710400,"low":41,"high":58,"text":"Partly Cloudy","code":30},{"day":"Thu","date":1608796800,"low":40,"high":59,"text":"Mostly Sunny","code":34}]}

代码中用到的maven依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.9</version>
        </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值