WeatherKit的接入

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文档

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值