利用电信发送模板短信功能

到开发文档下的发送模板短信下载模板示例代码


用ideal 建一个maven的java工程不选择webapp

类SendSMSRandcode是获取access _token令牌的, TemplateSms利用app_id,   app_secret,  access _token令牌发送短信

注意:app_id,   app_secret,  是电信开放平台中的管理中心,我的应用,应用设置中开发平台给的参数(创建应用并且创建成功后才有的)

package ffcs.sms;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import ffcs.util.Getproperties;
import ffcs.util.HttpInvoker;
import ffcs.util.RandomUtil;
import open189.sign.ParamsSign;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

/**
 * 下发自定义短信验证码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();//自定义验证码
   {
      APP_ID = Getproperties.getProperties().getString("appId");
      APP_SECRET =  Getproperties.getProperties().getString("appSecret");
   }
// /**
//  * 1 获取信任码token get提交
//  * 2自定义短信验证码下发 post提交
//  * @param userPhone 下发手机号
//  * @return
//  * @throws Exception
//  */
   //第一步根据app_idapp_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", "2");

      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("15159501225");
             System.out.println(result);
         }finally{

      }

}


}

获取到令牌

package ffcs.sms;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import ffcs.util.Getproperties;
import ffcs.util.HttpInvoker;
import ffcs.util.RandomUtil;

import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 模板短信DEMO
 *
 */
public class TemplateSms {
   public  String APP_ID ;//应用ID------登录平台在应用设置可以找到
   public  String APP_SECRET ;//应用secret-----登录平台在应用设置可以找到
   public  String ACCESS_TOKEN ;//访问令牌AT-------CC模式,AC模式都可,推荐CC模式获取令牌
   public  String TEMPLATE_ID ;//模板ID

   {
      APP_ID = Getproperties.getProperties().getString("appId");
      APP_SECRET = Getproperties.getProperties().getString("appSecret");
      ACCESS_TOKEN = Getproperties.getProperties().getString("accessToken");
      TEMPLATE_ID = Getproperties.getProperties().getString("templateId");
   }
// //第一步根据app_idapp_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  String sendSms(String tel) throws Exception {
      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", "xxx");
      map.put("param2", RandomUtil.randomFor6());
      map.put("param3","3" );
      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 (NullPointerException e){
         e.printStackTrace();
      } catch (IOException e) {
         System.err.println(resJson);
         e.printStackTrace();
      } catch (Exception e) {
         System.err.println(resJson);
         e.printStackTrace();
      }
      System.err.println(resJson);
      return idertifier;

   }

   /**
    * @param args
    */
   public static void main(String[] args) {
      String result = "";
      TemplateSms templateSms=new TemplateSms();
      try {
            result = templateSms.sendSms("13799990023");
            System.out.println(result);
      }catch (Exception e) {
         e.printStackTrace();
      }
   }

}

appId=xxxxxxxxxxxxxxxxxxxxxxxx
appSecret=xxxxxxxxxxxxxxxxx
accessToken=xxxxxxxxxxxxxxxxx
templateId=xxxxxxxxxx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值