自定义短信验证码
package com.ffcs.sms; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import java.util.TreeMap; import com.ffcs.util.HttpInvoker; import com.ffcs.util.RandomUtil; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import open189.sign.ParamsSign; /** * 下发自定义短信验证码DEMO * */ public class SendSMSRandcode { public static String APP_ID = "";//应用ID------登录平台在应用设置可以找到 public static String APP_SECRET = "";//应用secret-----登录平台在应用设置可以找到 public static String ACCESS_TOKEN = "";//访问令牌AT-------CC模式,AC模式都可,推荐CC模式获取令牌 public static String RANDCODE = RandomUtil.randomFor6();//自定义验证码 /** * 1 获取信任码token get提交 * 2自定义短信验证码下发 post提交 * @param userPhone 下发手机号 * @return * @throws Exception */ //第一步根据app_id,app_secret获取令牌接口 private static String getAccess_Token() throws Exception { Gson gson = new Gson(); String postUrl = "https://oauth.api.189.cn/emp/oauth2/v3/access_token?grant_type=client_credentials&app_id=" + APP_ID + "&app_secret=" + APP_SECRET; String resJson1 = HttpInvoker.httpPost(postUrl, null, null); System.err.println(resJson1); Map<String, String> map1 = gson.fromJson(resJson1, new TypeToken<Map<String, String>>() { }.getType()); return map1.get("access_token").toString(); } private static String sendSms(String userPhone) throws Exception { ACCESS_TOKEN=getAccess_Token(); System.err.println(ACCESS_TOKEN); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String timestamp = dateFormat.format(date); System.err.println(timestamp); TreeMap<String, String> paramsMap = new TreeMap<String, String>(); paramsMap.put("app_id", APP_ID); paramsMap.put("access_token", ACCESS_TOKEN); paramsMap.put("timestamp", timestamp); String getUrl = "http://api.189.cn/v2/dm/randcode/token?app_id=" + APP_ID + "&access_token=" + ACCESS_TOKEN + "×tamp="+URLEncoder.encode(timestamp,"UTF-8") + "&sign="+ParamsSign.value(paramsMap, APP_SECRET); System.out.println(getUrl); String resJson = HttpInvoker.httpGet(getUrl,null); System.err.println(resJson); Gson gson = new Gson(); Map<String, String> map = gson.fromJson(resJson, new TypeToken<Map<String, String>>() {}.getType()); System.out.println(map.get("token"));//获取信任码 //----------------------------------------------------------- TreeMap<String, String> paramsMap1 = new TreeMap<String, String>(); paramsMap1.put("app_id", APP_ID); paramsMap1.put("access_token", ACCESS_TOKEN); paramsMap1.put("timestamp", timestamp); paramsMap1.put("token", map.get("token").toString()); paramsMap1.put("randcode", RANDCODE); paramsMap1.put("phone", userPhone); paramsMap1.put("exp_time", "20"); String postUrl = "http://api.189.cn/v2/dm/randcode/sendSms"; System.out.println(map.get("token")); String postEntity = "app_id="+APP_ID + "&access_token="+ACCESS_TOKEN + "&token=" + map.get("token").toString() + "&phone=" + userPhone + "&randcode=" + RANDCODE + "&exp_time=2" + "×tamp="+ URLEncoder.encode(timestamp, "UTF-8") + "&sign="+ParamsSign.value(paramsMap1, APP_SECRET); System.out.println(postEntity); String resJson1 = HttpInvoker.httpPost(postUrl,null,postEntity); Map<String,String> map2=gson.fromJson(resJson1, new TypeToken<Map<String, String>>() {}.getType()); System.out.println(resJson1); String result=map2.get("identifier").toString(); System.out.println(resJson1); return result; } public static void main(String[] args) throws Exception { try { String result=sendSms("1583456732"); System.out.println(result); }finally{ } } } /* 最后返回参数: 短信标识码:"identifier":"no2337" unix时间戳 :"create_at":"1435569920" */
模板短信
package com.ffcs.sms; import java.io.IOException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import com.ffcs.util.HttpInvoker; import com.ffcs.util.RandomUtil; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; /** * 模板短信DEMO * */ public class TemplateSms { public static String APP_ID = "";//应用ID------登录平台在应用设置可以找到 public static String APP_SECRET = "";//应用secret-----登录平台在应用设置可以找到 public static String ACCESS_TOKEN = "";//访问令牌AT-------CC模式,AC模式都可,推荐CC模式获取令牌 public static String TEMPLATE_ID = "";//模板ID //模板参数 public static String CUSTOMER= "XXXX";//param1客户 public static String RANDCODE = RandomUtil.randomFor6();//param2验证码 public static String EXP_TIME="2";//param3单位分钟 //第一步根据app_id,app_secret获取令牌接口 private static String getAccess_Token() throws Exception { Gson gson = new Gson(); String postUrl = "https://oauth.api.189.cn/emp/oauth2/v3/access_token?grant_type=client_credentials&app_id=" + APP_ID + "&app_secret=" + APP_SECRET; String resJson1 = HttpInvoker.httpPost(postUrl, null, null); System.err.println(resJson1); Map<String, String> map1 = gson.fromJson(resJson1, new TypeToken<Map<String, String>>() { }.getType()); return map1.get("access_token").toString(); } public static String sendSms(String tel) throws Exception { ACCESS_TOKEN=getAccess_Token(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); String timestamp = dateFormat.format(date); System.err.println(timestamp); Gson gson = new Gson(); Map<String, String> map = new HashMap<String, String>(); //这里存放模板参数,如果模板没有参数直接用template_param={} map.put("param1", CUSTOMER); map.put("param2", RANDCODE); map.put("param3",EXP_TIME); String template_param = gson.toJson(map); System.out.println(template_param); String postUrl = "http://api.189.cn/v2/emp/templateSms/sendSms"; String postEntity = "app_id=" + APP_ID + "&access_token=" + ACCESS_TOKEN + "&acceptor_tel=" + tel + "&template_id=" + TEMPLATE_ID + "&template_param=" + template_param + "×tamp=" + URLEncoder.encode(timestamp, "utf-8"); System.out.println(postUrl); System.out.println(postEntity); String resJson = ""; String idertifier = null; Map<String, String> map2 =null; try { resJson = HttpInvoker.httpPost1(postUrl, null, postEntity); map2 = gson.fromJson(resJson, new TypeToken<Map<String, String>>() { }.getType()); idertifier = map2.get("idertifier").toString(); } catch (IOException e) { System.err.println(resJson); e.printStackTrace(); } catch (Exception e) { System.err.println(resJson); e.printStackTrace(); } System.err.println(resJson); return idertifier; } /* * 响应结果示例: { "res_code": "0", "res_message": "Success", "identifier": * "000000001" } */ /** * @param args */ public static void main(String[] args) { String result = ""; try { result = sendSms("1585343432"); System.out.println(result); }catch (Exception e) { e.printStackTrace(); } } }
具体程序代码见附件:
本文出自 “一无所有-天行者” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1669714
原文地址:http://tianxingzhe.blog.51cto.com/3390077/1669714