package com.shuhaiwifi.web.auth2.iot; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.nianzai.util.Tools; import cn.hutool.crypto.SecureUtil; import cn.hutool.http.HttpUtil; public class ThirdToken { private static final Log logger = LogFactory.getLog(CmApi.class); public static void main(String[] args) { String iccid="898608352325D0094600"; String token=getToken(2,iccid); System.out.println(token); CmApi.getTerminalList(token,iccid,"9C-E5-49-E0-4D-65"); // String iccid="89860441192580181041"; // String token=getToken(1,iccid); // System.out.println(token); // //CmApi.getTerminalList(token,iccid,"18-60-24-8F-4D-AD"); // CmApi.sendSms(token, iccid, "18-60-24-8F-4D-AD", "18-10-24-8F-4D-AD", "13501078357"); } public static String getToken(int kasid,String iccid) { String token =""; if(kasid==Iot.bw) { String url="https://dmp.combmobile.com/mifi-portal/api/getOneLinkAuthToken"; Map m=new HashMap(); m.put("iccid", iccid); String content=HttpUtil.createPost(url).body(JSONObject.toJSONString(m), "application/json").execute().body(); JSONObject resJson = JSONObject.parseObject(content); boolean code=resJson.getBoolean("success"); if(code) { resJson = JSONObject.parseObject(resJson.getString("data")); token=resJson.getString("token"); } } else if(kasid==Iot.yl) { try { String appKey="1ca4d68a50d10ce9b9ba9334a1c90eea"; String appSecret="df53a52ca350494ba94ca5f4daa1204f"; String appid="C5010851A85156636691"; String url="https://iotapi.apika.cn/iot-api/wireless/auth/refreshWirelessToken"; String sign=getSignature(appKey,appSecret); Map m=new HashMap(); m.put("appid", appid); Map map = new HashMap(); map.put("appKey", appKey); map.put("appSecret", appSecret); map.put("signature", sign); map.put("sign", sign); logger.info(url); String content=HttpUtil.createPost(url).addHeaders(map).body(JSONObject.toJSONString(m), "application/json").execute().body(); logger.info(content); JSONObject resJson = JSONObject.parseObject(content); int code=resJson.getIntValue("code"); if(code==200) { resJson = JSONObject.parseObject(resJson.getString("data")); token=resJson.getString("token"); } } catch(Exception e) { logger.info(Tools.getExceptionMessage(e)); } } if(kasid==Iot.ylqg)//亿联4G移动(全国移动) { String appKey="1ca4d68a50d10ce9b9ba9334a1c90eea"; String appSecret="df53a52ca350494ba94ca5f4daa1204f"; String appid="C5010851A85156636691"; String url="https://iotapi.apika.cn/iot-api/wireless/auth/refreshWirelessToken"; String sign=getSignature(appKey,appSecret); Map m=new HashMap(); m.put("appid", appid); Map map = new HashMap(); map.put("appKey", appKey); map.put("appSecret", appSecret); map.put("signature", sign); logger.info(url); String content=HttpUtil.createPost(url).addHeaders(map).body(JSONObject.toJSONString(m), "application/json").execute().body(); logger.info("ylqg>>>"+content); JSONObject resJson = JSONObject.parseObject(content); int code=resJson.getIntValue("code"); if(code==200) { resJson = JSONObject.parseObject(resJson.getString("data")); token=resJson.getString("token"); } } if(kasid==Iot.lfgd)//科锋广东 { String url = "http://120.76.136.43:18888/iotcard/sys/api/getToken?name=ssly&sing=527C8E05E4B21647FD21CEA1FBC1668D&time=1678340234&aisleid=28997376438029133"; logger.info(url); token= HttpUtil.get(url); } if(kasid==Iot.wx || kasid==Iot.wx2)//五兴4G移动卡 { String url = "https://api.wxkjwlw.com/api/v2/outputApi/operatorRealNameAuthToken"; logger.info(url); String appKey="VO1NFpu75CHaC9WY2Yc31wkS6EM1IVvd"; String appSecret="f06825dc52664f6cb0ac1153be4d5851"; Map m = new TreeMap<>(); m.put("operator", 2); m.put("operatorPlatformType", 1); m.put("operatorPlatformKey", kasid==Iot.wx?"C5010200A9992001846946":"C5010200A9992001846023"); m.put("appkey", appKey); m.put("timestamp", System.currentTimeMillis()/1000); m.put("nonce", (int) (Math.random() * 90000) + 10000); String signstr = convertToQueryString(m)+"&secret="+appSecret; String sign = sha1(signstr).toUpperCase(); m.put("sign", sign); logger.info("wuxing req>>>"+JSONObject.toJSONString(m)); String content=HttpUtil.createPost(url).body(JSONObject.toJSONString(m), "application/json").execute().body(); logger.info("wuxing resp>>>"+content); JSONObject resJson = JSONObject.parseObject(content); int code=resJson.getIntValue("code"); if(code==0) { resJson = JSONObject.parseObject(resJson.getString("data")); token=resJson.getString("token"); logger.info(token); } } if(kasid==Iot.yljs)//亿联4G移动(江苏移动) { String url = "http://ecrz.yeadry.com/api/v5/wireless/get/token/appid?appid=b0758ea013f147f1a7ed0bcce3c233ff"; logger.info("yljs>>>"+url); String content= HttpUtil.get(url); content=content.trim(); logger.info(content); JSONObject resJson = JSONObject.parseObject(content); int code=resJson.getIntValue("code"); if(code==0) { resJson = JSONObject.parseObject(resJson.getString("result")); token=resJson.getString("token"); logger.info(token); } } if(kasid==Iot.zy)//中亿4G移动卡 { String url = "https://api.aiotzy.com/api/auth/oauth/token?grant_type=open_api&appId=zyO3OQ3uTr&appSecret=c451942cfc9a6e52b5711c4f1e24ceaa418b456f"; logger.info("zy oauth>>>"+url); String content= HttpUtil.post(url,""); content=content.trim(); logger.info(content); JSONObject resJson = JSONObject.parseObject(content); JSONObject result=resJson.getJSONObject("data"); if(result!=null) { String tokenHead=result.getString("tokenHead"); String accessToken=result.getString("accessToken"); url = "https://api.aiotzy.com/api/terminal/open/v1/terminalCard/queryAuthToken"; logger.info("zy queryAuthToken>>>"+url); Map head = new HashMap<>(); Map body = new HashMap<>(); head.put("Authorization", tokenHead+accessToken); body.put("terminal", iccid); body.put("forceRefreshToken", 1); content=HttpUtil.createPost(url).addHeaders(head).body(JSONObject.toJSONString(body), "application/json").execute().body(); logger.info(content); resJson = JSONObject.parseObject(content); result=resJson.getJSONObject("data"); if(result!=null) { token=result.getString("token"); logger.info(token); } } } if(kasid==Iot.bmbjyd || kasid==Iot.bmlcyd || kasid==Iot.bmgdyd) { String appId="",openId="",channelId=""; if (kasid==Iot.bmbjyd) { appId="C5010100A23100111720247"; openId="GFLaxxfVMG4DGkgcdZEEg4zexmje3dq4"; channelId="236"; } else if (kasid==Iot.bmlcyd){ appId="C5010531Aq5201817914"; openId="GFLaxxfVMG4DGkgcdZEEg4zexmje3dq4"; channelId="216"; } else { appId="C5010200A9992002368004"; openId="GFLaxxfVMG4DGkgcdZEEg4zexmje3dq4"; channelId="239"; } Map receivedData = new HashMap(); String times=String.valueOf(System.currentTimeMillis()/1000); receivedData.put("times", times); receivedData.put("appId", appId); receivedData.put("channelId", channelId); TreeMap sortedData = new TreeMap<>(receivedData); // 构建待签名字符串 StringBuilder stringA = new StringBuilder(); stringA.append(openId); for (Map.Entry entry : sortedData.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (value != null && !value.isEmpty() && !"sign".equals(key) ) { stringA.append(key).append(value); } } // 拼接key,并进行MD5运算 stringA.append(openId); System.out.println(stringA.toString()); String signValue = SecureUtil.sha1((stringA.toString())).toUpperCase(); String url="http://db.jinmaiiot.com:7021/api/external/getDeviceToken?appId="+appId+"&openId="+openId+"×="+times+"&sign="+signValue+"&channelId="+channelId; logger.info("bmyd>>>"+url); String content= HttpUtil.get(url); logger.info(content); JSONObject resJson = JSONObject.parseObject(content); JSONArray result=resJson.getJSONArray("result"); int status=resJson.getIntValue("status"); if(status==0) { resJson=JSONObject.parseObject(String.valueOf(result.get(0))); token=resJson.getString("token"); logger.info(token); } } if(kasid==Iot.lxxj || kasid==Iot.lxyn || kasid==Iot.lxqh || kasid==Iot.lxgz) { String appId=""; if (kasid==Iot.lxxj) { appId="C5010991A9912845696"; }else if (kasid==Iot.lxyn) { appId="C5010871A87111730331"; }else if (kasid==Iot.lxqh) { appId="C5010971A7100567389"; } else { appId="C5010851A85151776534"; } String url="http://ecrz.szsgsxc.cn/api/v5/wireless/get/token/appid?appid="+appId; logger.info("lxyd>>>"+url); String content= HttpUtil.get(url); logger.info(content); JSONObject resJson = JSONObject.parseObject(content); JSONObject result=resJson.getJSONObject("result"); String code=resJson.getString("code"); if("0".equals(code)) { token=result.getString("token"); logger.info(token); } } if(kasid==Iot.whhnyd) { String url = "https://shouhuapi.yimingkeji.net/polling/v1/channel/channleAuthToken"; logger.info("whhnyd queryAuthToken>>>"+url); Map body = new HashMap<>(); body.put("appid", "7112100442210304"); String content=HttpUtil.createPost(url).body(JSONObject.toJSONString(body), "application/json").execute().body(); logger.info("whhnyd>>"+content); JSONObject resJson = JSONObject.parseObject(content); JSONObject result=resJson.getJSONObject("result"); String code = resJson.getString("code"); if ("0".equals(code) && result != null) { token = result.getString("token"); logger.info(token); } } if(kasid==Iot.bw) { String url="https://dmp.combmobile.com/mifi-portal/api/getOneLinkAuthToken"; Map m=new HashMap(); m.put("iccid", iccid); String content=HttpUtil.createPost(url).body(JSONObject.toJSONString(m), "application/json").execute().body(); JSONObject resJson = JSONObject.parseObject(content); boolean code=resJson.getBoolean("success"); if(code) { resJson = JSONObject.parseObject(resJson.getString("data")); token=resJson.getString("token"); } } return token; } public static String sha1(String input) { if (input == null) { throw new IllegalArgumentException("输入不能为null"); } MessageDigest digest = null; try { digest = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } byte[] hashBytes = digest.digest(input.getBytes()); StringBuilder sb = new StringBuilder(); for(byte b : hashBytes) { String hex = Integer.toHexString(0xff & b); if(hex.length() == 1) sb.append('0'); sb.append(hex); } return sb.toString(); } public static String convertToQueryString(Map map) { if (map == null || map.isEmpty()) { return ""; } StringBuilder result = new StringBuilder(); Iterator> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry entry = iterator.next(); String key = entry.getKey(); String value = String.valueOf(entry.getValue()); // 跳过空值 if (value == null || value.trim().isEmpty()) { continue; } result.append(key).append("=").append(value); if (iterator.hasNext()) { result.append("&"); } } return result.toString(); } /** * 获取签名 * * @param appKey * @param appSecret * @return */ public static String getSignature(String appKey, String appSecret) { String signStr=""; String data = "{" + appKey + "}:{" + appSecret + "}"; byte[] bytes = null; try { MessageDigest md = MessageDigest.getInstance("SHA-256"); bytes = md.digest(data.getBytes(StandardCharsets.UTF_8)); StringBuilder sign = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length() == 1) { sign.append("0"); } sign.append(hex.toUpperCase()); } signStr = Base64.getEncoder().encodeToString(sign.toString().getBytes(StandardCharsets.UTF_8)); // System.out.println("获取出来的签名为: {}"+ signStr); } catch (Exception gse) { //System.out.println("获取签名出错 {}"+gse.getCause().toString()); } return signStr; } }