Custom SMS Verification Code
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;/** * Definition SMS Verification code demo * */public class SendSMSRandcode{ public static String APP_ID = "" ;//App ID------Login platform can find public static String APP_SECRET = "" in app settings;// Apply Secret-----Login platform can find public static String ACCESS_TOKEN = "" in app settings;// Access token at-------cc mode, AC mode, recommended cc mode for token public static String RANDCODE = Randomutil.randomfor6 ();//Custom CAPTCHA /** * 1 Get Trust code Token get submit * 2 Custom SMS Verification code issued post submitted * @param userPhone issued mobile phone number * @return * @throws exception */ //First step according to App_id,app_ Secret Get Token Interface 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"));//Get the Trust code //----------------------------------------------------------- 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", "); 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 " + " & Timestamp= "+ 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{ }} }/* last return parameter: SMS Identification code: "identifier": "no2337" Unix timestamp : "Create_at": " 1435569920 " */
Template text message
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;/** * Template SMS demo * */public class templatesms { public static string app_id = "";//App ID------Login platform can be found in app settings public static String APP_SECRET = "";//Application SECRET-----Login platform can be found in application settings public static String ACCESS_TOKEN = "";//access token at-------cc mode, AC mode, recommended cc mode to get token public static String TEMPLATE_ID = "";//template id //template Parameters public static string customer= "XXXX";//param1 customer public static String RANDCODE = Randomutil.randomfor6 ();//param2 Verification Code &NBSp;public static string exp_time= "2";//param3 unit minute //first step based on App_id,app_secret get token interface 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); &NBSP;SYSTEM.ERR.PRINTLN (timestamp); gson gson = new gson (); Map< String, string> map = new hashmap<string, string> (); // Here the template parameters are stored, if the template does not have parameters directly with Template_param={} map.put ("param1", customer); map.put ("Param2" , randcode); map.put ("Param3", Exp_time); string template_param = Gson.tojson (map); &NBSP;&NBSp 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; } /* * Response result Example: { "Res_code": "0", "res_message": "Success", "identifier": * "000000001" } */ /** * @param args */ public static void main (String[] args) { String result = "; try { result =&Nbsp;sendsms ("1585343432"); system.out.println (result); }catch ( exception e) { e.printstacktrace (); } }}
The specific program code is shown in the annex:
This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1669714
China Telecom Sky-wing open platform custom SMS Verification code and template SMS Demo