WeatherKit的接入
进入苹果官网购买Apple Developer Program 会员才能使用 如果没有直接退出页面 接不了一点儿
https://developer.apple.com/cn/weatherkit/get-started/
weatherKit链接里有接口的说明文档
我们主要配置密钥这一块
进去新建一个服务 和一个key
这里官方有详细的说明 需要注意一定要勾选你要用的服务weatherKit
前置工作做好了 才能开始后面部分
这里是配置类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* WeatherKitApiClientProperties
*/
@Data
@Configuration(proxyBeanMethods = false)
@ConfigurationProperties(prefix = "client.weather-kit")
public class WeatherKitApiClientProperties {
/**
* 签名方式,Apple要求,必须为“ES256”
*/
private String alg="ES256";
/**
* 创建的Key的ID
*/
private String kid;
/**
* 是Team ID和bundle ID 的拼接
*/
private String id;
/**
* JWT签发者,值为你的Team ID
*/
private String iss;
/**
* app的 bundle ID
*/
private String sub;
/**
* 私钥
*/
private String privateKey;
}
yml配置
url:
weather-kit: https://weatherkit.apple.com
client:
weather-kit:
kid: 你的Key ID
iss: 你的Team ID
sub: 你的bundle ID
private-key: |-
你的privateKey
这里是增加请求头header中Authorization的参数方法 直接copy到你的代码中参考使用
public static final String SCOPE = "WK";
private final WeatherKitApiClientProperties weatherKitApiClientProperties;
private static final String ALGORITHM = "EC";
private static ECPrivateKey privateKey;
private static final Long expiration = 7200L; //这个就是过期时间 秒为单位
protected void applyInterval(RequestTemplate requestTemplate, String allowScope) {
//在此之前可以用redis做个缓存 我这里就不给大家写怎么做了
WeatherKitApiClientProperties weatherProperties = weatherKitApiClientProperties;
JSONObject head = new JSONObject();
head.put("arg", weatherProperties.getAlg());
head.put("kid", weatherProperties.getKid());
head.put("id", weatherProperties.getIss() +"."+ weatherProperties.getSub());
JSONObject payLoad = new JSONObject();
payLoad.put("iss", weatherProperties.getIss());
//获取当前时间的总秒数
long epochSecond = LocalDateTime.now().atZone(ZoneOffset.systemDefault()).toEpochSecond();
//JWT签发时间
payLoad.put("iat", epochSecond);
//JWT过期时间
payLoad.put("exp", epochSecond + expiration);
payLoad.put("sub", weatherProperties.getSub());
Algorithm algorithm=Algorithm.ECDSA256(privateKey);
string = JWT.create().withHeader(head).withPayload(payLoad).sign(algorithm);
requestTemplate.header("Authorization", "Bearer " + string);
}
调用苹果的方法这里完全可以自己写了
import com.alpha.cdrd.apollo.equip.interecptor.WeatherKitFeignInterceptor;
import com.alpha.cdrd.apollo.equip.client.model.weather.response.WeatherKitResponse;
import com.alpha.cdrd.cloud.framework.httpclient.DisableContext;
import com.alpha.cdrd.cloud.framework.httpclient.ScopeFeignRequestInterceptor;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Set;
@FeignClient(contextId = "WeatherKitFeignClient", name = "weather-kit", url = "${url.weather-kit}")
public interface WeatherKitClient {
/**
* 查询苹果天气数据
*
* @param language 语言
* @param latitude 经纬度
* @param longitude 经纬度
* @param dataSets 数据key
* @param dailyEnd 结束日期
* @return {@link WeatherKitResponse}
*/
@GetMapping(value = "/api/v1/weather/{language}/{latitude}/{longitude}")
WeatherKitResponse weather(@PathVariable("language") String language
, @PathVariable("latitude") String latitude
, @PathVariable("longitude") String longitude
, @RequestParam("dataSets") Set<String> dataSets
, @RequestParam("dailyEnd") String dailyEnd);
}
也可以自己用HttpOk来做 主要在于注册和校验的token的生成
以下是postman使用生成的token进行请求
获取可用的数据集
将数据集参数传入进去就可以拿到对应的数据集 具体看官网的api文档