中國電信天翼開放平台自訂簡訊驗證碼和模板簡訊demo

來源:互聯網
上載者:User

標籤:中國天翼開放平台自訂簡訊驗證碼和模板簡訊demo

自訂簡訊驗證碼

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     + "&timestamp="+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"              + "&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{  }}  }/*最後返回參數:簡訊標識碼:"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    + "&timestamp=" + 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

中國電信天翼開放平台自訂簡訊驗證碼和模板簡訊demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.