1.登录接口涉及Authorization请求验证,爬取天气接口BearerAuth token验证
log.info("每小时十分钟获取天气信息");
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add(AirConst.USERNAME, username);
map.add(AirConst.PASSWORD, password);
map.add(AirConst.GRANT_TYPE, grant_type);
map.add(AirConst.SCOPE, scope);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
headers.add("Authorization", "Basic dGFscm9hZDp0YWxyb2Fk");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(map,
headers);
ResponseEntity<JSONObject> tokenResp = restTemplate.exchange(air_token_url, HttpMethod.POST, httpEntity, JSONObject.class);
HttpStatus statusCode = tokenResp.getStatusCode();
if (statusCode.value() == 200) {
JSONObject tokenBody = tokenResp.getBody();
Integer code = tokenBody.getInteger(AirConst.CODE);
if (code == 0) {
JSONObject data = tokenBody.getJSONObject(AirConst.DATA);
HttpHeaders headers2 = new HttpHeaders();
headers2.setBearerAuth(data.getString(AirConst.ACCESS_TOKEN));
headers2.add(AirConst.REGION_CODE, data.getString(AirConst.REGION_CODE));
headers2.add(AirConst.REGION_LEVER, data.getString(AirConst.REGION_LEVER));
headers2.add(AirConst.PARENT_CODE, AirConst.PARENT_CODE_VALUE);
HttpEntity<String> httpEntity1 = new HttpEntity<>(headers2);
ResponseEntity<JSONObject> airResp = restTemplate.exchange(air_weather_url, HttpMethod.GET, httpEntity1, JSONObject.class);
HttpStatus airStatusCode = airResp.getStatusCode();
if (airStatusCode.value() == 200) {
//TODO:
} else {
log.error("获取气象数据失败");
}
} else {
log.error(tokenBody.getString("msg"));
}
} else {
log.error("获取token失败");
}
版权声明:本文为weixin_37460672原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。