1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 发送短信验证码(天翼开放平台)

发送短信验证码(天翼开放平台)

时间:2018-12-08 01:55:48

相关推荐

发送短信验证码(天翼开放平台)

此文章使用天翼开发平台(模板短信)接口

API请求地址:

/v2/emp/templateSms/sendSms

响应格式:

POST

请求参数:

请求参数示例:

POST /v2/emp/templateSms/sendSms

POST data:

acceptor_tel=13123185312&template_id=91000001&template_param={"日报":"nihao","晚报":"nidao","url":""}&app_id=418839000000031xxx&access_token=c49fabf158e25985ed1284a75716a9b9137067210xxxx&timestamp=-09-06+16%3A07%3A42

响应结果:

响应参数说明:

res_code:同步结果代码,返回0表示成功,其它值标识失败,后续在定义

res_message:成功返回:Success;错误返回:错误信息

identifier:成功返回:短信唯一标识;错误返回:返回空

响应结果示例:

JSON

{"res_code": "0","res_message": "Success","identifier": "000000001"}

令牌接口:

请求地址:

https://oauth./emp/oauth2/v3/access_token

承载协议:

HTTPS

请求方式:

POST

请求范例:CC授权模式下的UIAT访问令牌获取

https://oauth./emp/oauth2/v3/access_token

使用POST方法传参:

grant_type=client_credentials&app_id=1234567890&app_secret=abcdefghijk

成功应答范例:返回UIAT访问令牌

Content-Type: application/json;charset=UTF-8Cache-Control: no-storePragma: no-cache{"access_token":"USER_INDEPENDENT_ACCESS_TOKEN","expires_in":9999,“res_code”:0,“res_message”:”Success”}

失败应答范例:

HTTP/1.1 400 Bad RequestContent-Type: application/json;charset=UTF-8Cache-Control: no-storePragma: no-cache{“res_code”:10009,“res_message”:”Access denied”}

以上都是天翼开发平台的一些文档帮助说明

下面是令牌接口与短信发送接口代码

令牌接口

public class AccessToken {//接口private static final String ACCESSTOKEN_URL = "https://oauth./emp/oauth2/v3/access_token";//此值必须为“client_credentials”private static final String GRANT_TYPE="client_credentials";//应用注册时分配的应用IDprivate static final String APP_ID="这里是你申请的APPID";//申请应用时分配的应用密钥private static final String APP_SECRET="这里是你申请的应用ID";// 用于跟踪调用状态。在响应消息中将会原封不动的返回该值private static String state = "";// 权限列表,保留字段,默认为空private static String scope = "";public static String getAccessToken() {String accessToken = "";try {URL postURL = new URL(ACCESSTOKEN_URL);HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);httpURLConnection.setRequestMethod("POST");httpURLConnection.setUseCaches(false);httpURLConnection.setInstanceFollowRedirects(true);httpURLConnection.setRequestProperty(" Content-Type ", " application/x-www-form-urlencoded ");String postEntity = "grant_type=" + GRANT_TYPE + "&app_id=" + APP_ID + "&app_secret=" + APP_SECRET;DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());out.writeBytes(postEntity);out.flush();out.close(); // flush and close// connection.connect();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));StringBuilder sbStr = new StringBuilder();String line;while ((line = bufferedReader.readLine()) != null) {sbStr.append(line);}bufferedReader.close();httpURLConnection.disconnect();//获取返回结果转成json 解析获取accesstoken值JSONObject resultJson =JSONObject.parseObject(new String(sbStr.toString().getBytes(), "utf-8"));String code=resultJson.get("res_code").toString();//成功 返回 Oif("0".equals(code)) {accessToken = resultJson.get("access_token").toString();}} catch (IOException e) {e.printStackTrace();}return accessToken;}// public static void main(String[] agrs) {// AccessToken accessToken=new AccessToken();// System.out.println(accessToken.getAccessToken());// }}

短信发送

public class SendSms {//发送验证信息请求地址private static final String SMS_URL = "/v2/emp/templateSms/sendSms";//应用ID,开发者创建应用时,天翼开放平台会为应用自动分配此ID,开发者进入"管理中心",在“应用详情”中可查看app_id。private static final String APP_ID = "这里是你申请的APPID";//短信模板ID,到短信模板申请页面查看private static final String TEMPLATE_ID="这里是你申请的短信模板ID";//截至时间格式private static final String DATEFORMAT="yyyy-MM-dd HH:mm:ss";//访问令牌,是调用平台能力接口的通行证,可通过调用"令牌接口"获得。private static String access_token = "";//接收方号码,不支持0打头的号码private static String acceptor_tel = "";//模板匹配参数,参数格式为(json对象字符串): {参数名:参数值,参数名:参数值}private static String template_param;//时间戳,格式为:yyyy-MM-dd HH:mm:ssprivate static String timestamp="";//参数签名private static String sign="";/*** 发送验证码,验证码有效时间3分钟* @param nickName* @param tel* @return*/public static String sendSMS(String nickName,String tel,int timeout) {JSONObject json=null;//令牌access_token = AccessToken.getAccessToken();//截至时间timestamp =getTimesTamp(timeout);//参数String code=createCode();template_param =setTemplateParam(nickName,code, timeout);//发送手机号acceptor_tel =tel;StringBuffer sb = new StringBuffer();sb.append("acceptor_tel="+acceptor_tel);sb.append("&template_id=" + TEMPLATE_ID);sb.append("&template_param=" + template_param);sb.append("&app_id=" + APP_ID);sb.append("&access_token=" + access_token);sb.append("&timestamp=" + timestamp);try {URL postURL = new URL(SMS_URL);HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);httpURLConnection.setRequestMethod("POST");httpURLConnection.setUseCaches(false);httpURLConnection.setInstanceFollowRedirects(true);httpURLConnection.setRequestProperty(" Content-Type ", " application/x-www-form-urlencoded ");DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());out.writeBytes(sb.toString());out.flush();out.close(); // flush and close// connection.connect();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));StringBuilder sbStr = new StringBuilder();String line;while ((line = bufferedReader.readLine()) != null) {sbStr.append(line);}bufferedReader.close();httpURLConnection.disconnect();json=JSONObject.parseObject(new String(sbStr.toString().getBytes(), "utf-8"));json.put("code",code);} catch (IOException e) {e.printStackTrace();}return json.toJSONString();}//设置参数//这里参数是你申请短信模板时候可以定义的参数//参数1 参数2 参数3 是我定义的参数//这里分别是 用户名 ,验证码,过期分钟数private static String setTemplateParam(String nickName,String code,int timeOut){JSONObject param=new JSONObject();param.put("参数1",nickName);param.put("参数2",code);param.put("参数3",timeOut);return param.toJSONString();}//获取截至时间private static String getTimesTamp(int timeout){Calendar nowTime = Calendar.getInstance();nowTime.add(Calendar.MINUTE, timeout);return new SimpleDateFormat(DATEFORMAT).format(nowTime.getTime());}//生成4位验证码private static String createCode() {int code=(int)(Math.random()*9000+1000);return code + "";}// public static void main(String [] args){// System.out.println(sendSMS("用户名", "手机号码", 过期时间数));// }

这就OK了

就写这么多吧,有什么问题可以留言,下班了,今天要搬家.苦逼啊…….~.~!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。