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

目录
相关文章
|
安全 数据安全/隐私保护
2022 年推荐免费在线接收短信平台(国内、国外)
现代社会中大多数人容易忘记密码,因此,为了方便,各大网站或者 APP 就相继出现以手机号码进行短信验证来注册和登录等操作。但此时,大多个人手机号码都已经是实名认证的,就非常怕存在个人信息泄露的情况。近几年网络平台用户数据泄露事件层出不穷,勿论一般平台,甚至一些全球知名企业也曾被曝出用户数据泄露问题,那基于此我们用户又能做点什么呢?
44327 0
2022 年推荐免费在线接收短信平台(国内、国外)
|
1月前
|
API 开发者
1688阿里巴巴中国站平台 API接口获取商品详情 接入文档说明
1688(阿里巴巴批发网)的API接入文档是专为开发者提供的,用于指导如何集成和使用1688平台提供的API接口。这些API接口可以帮助开发者实现各种功能,如商品搜索、订单管理、用户认证等。
|
1月前
|
数据安全/隐私保护
最新2024阿里云国际站最便捷注册流程
阿里云国际分销商TG@cloudicc
40 0
最新2024阿里云国际站最便捷注册流程
|
3月前
|
JSON 开发工具 数据格式
App Inventor 2 接入阿里云短信服务,实现短信验证码功能
App Inventor 2 接入阿里云短信服务,实现短信验证码功能:发送短信验证码功能一般都是基于短信平台提供的sdk进行调用,这里是基于阿里云短信平台进行的开发。
113 1
|
11月前
|
API
短信接口平台和短信运营商怎么选?
现今有许多人都在使用短信平台,包括企业和个体经营者,相信很多人在使用的过程中都听说过短信接口平台和短信运营商,那这两者有何区别?怎么选?
130 0
|
10月前
|
数据安全/隐私保护
阿里云国际站注册下单流程
阿里云国际站注册下单流程充值小飞机pang2020
|
小程序
微信小程序打造本地宝(3)——个人中心
微信小程序打造本地宝(3)——个人中心
263 0
微信小程序打造本地宝(3)——个人中心
|
测试技术 API 开发工具
饿了么开放平台接入(1)——开放平台注册与使用
本文目录 0. 序言 1. 背景 2. 注册账户 3. 资质认证 4. 创建应用 4.1 应用的概念 4.2 应用种类 4.3 应用的具体信息 5. 应用的测试环境(沙箱环境)与正式环境 5.1 沙箱环境概念 5.2 查看正式环境与沙箱环境 5.3 沙箱环境参数解释 6. 下一步工作
731 0
饿了么开放平台接入(1)——开放平台注册与使用
云支付代签约功能(云支付服务商助手)接入流程
云支付代签约功能(云支付服务商助手)接入流程
6881 0
云支付代签约功能(云支付服务商助手)接入流程
|
小程序 API Windows
云支付-支付宝信息配置流程
本帖主要是和大家分享一下,云支付创建应用至配置信息完成授权的详细流程
7884 0
云支付-支付宝信息配置流程