中国电信天翼开放平台自定义短信验证码和模板短信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.HttpInvoke

自定义短信验证码

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

目录
相关文章
|
2月前
|
API 开发者
1688阿里巴巴中国站平台 API接口获取商品详情 接入文档说明
1688(阿里巴巴批发网)的API接入文档是专为开发者提供的,用于指导如何集成和使用1688平台提供的API接口。这些API接口可以帮助开发者实现各种功能,如商品搜索、订单管理、用户认证等。
|
4月前
|
JSON 开发工具 数据格式
App Inventor 2 接入阿里云短信服务,实现短信验证码功能
App Inventor 2 接入阿里云短信服务,实现短信验证码功能:发送短信验证码功能一般都是基于短信平台提供的sdk进行调用,这里是基于阿里云短信平台进行的开发。
116 1
|
10月前
阿里云短信服务价格_企业短信营销推广_验证码通知-阿里云
阿里云短信服务价格_企业短信营销推广_验证码通知-阿里云,阿里云短信服务价格表,阿里云短信0.032元一条,阿里云短信价格?阿里云短信怎么收费?阿里云短信多少钱一条,阿里云短信价格0.032元一条
115 0
|
12月前
|
API
短信接口平台和短信运营商怎么选?
现今有许多人都在使用短信平台,包括企业和个体经营者,相信很多人在使用的过程中都听说过短信接口平台和短信运营商,那这两者有何区别?怎么选?
132 0
|
12月前
|
机器学习/深度学习 自然语言处理 安全
《阿里云云通信短信服务安全白皮书》——安全架构——七、 内容安全
《阿里云云通信短信服务安全白皮书》——安全架构——七、 内容安全
166 0
|
文字识别 安全 前端开发
全面对接教程——手机实名认证接口说明
为了让您更清晰了解手机三要素实名认证接口的功能与使用。深智恒际特意列了以下5个方面,引导您便捷对接API接口。即使是非技术人员,也可以借用postman工具进行调试使用。
全面对接教程——手机实名认证接口说明
|
安全 大数据 网络安全
阿里云国际版免实名注册账号,支持多种支付方式
阿里云国际版账号是需要通过阿里云渠道商来实现注册认证的,本篇文章就介绍一家这样的阿里云国际版渠道商1788yun.com,通过它,实现无需PayPal信用卡,无门槛注册买阿里云国际版的产品与服务。
阿里云国际版免实名注册账号,支持多种支付方式
《云市场-友盟+智能认证U-Verify》电子版地址
《云市场-友盟+智能认证U-Verify》PPT
107 0
《云市场-友盟+智能认证U-Verify》电子版地址
|
16天前
|
API 开发工具 图形学
Unity接入阿里云短信服务实现游戏手机号验证
本文介绍如何使用阿里云短信服务,调用API实现手机号验证等功能。
|
测试技术 API 开发工具
饿了么开放平台接入(1)——开放平台注册与使用
本文目录 0. 序言 1. 背景 2. 注册账户 3. 资质认证 4. 创建应用 4.1 应用的概念 4.2 应用种类 4.3 应用的具体信息 5. 应用的测试环境(沙箱环境)与正式环境 5.1 沙箱环境概念 5.2 查看正式环境与沙箱环境 5.3 沙箱环境参数解释 6. 下一步工作
748 0
饿了么开放平台接入(1)——开放平台注册与使用

热门文章

最新文章