NodeJS实现阿里大鱼短信通知发送
1、说明
############################################ 发该贴时签名实现方式粗糙,后边进行了改善并封装了阿里大鱼的sdk 看帖的朋友请移步alidayujs-demo 或者看npm的插件alidayujs ############################################
2、代码实现
生成sign
/**
* 根据淘宝官网提供的教程
* http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1
*/
var md5 = require("blueimp-md5");
// 淘宝应用App信息(换成自己的)
var config = {
AppKey: '233002**',
AppSecret: '3403636b338e100399**'
};
exports.config=config;
var dySign = function (obj) {
// 生成时间戳
var time = new Date();
var timestamp = time.getFullYear() + "-" +
("0" + (time.getMonth() + 1)).slice(-2) + "-" +
("0" + time.getDate()).slice(-2) + ' ' +
("0" + time.getHours()).slice(-2) + ":" +
("0" + time.getMinutes()).slice(-2) + ":" +
("0" + time.getSeconds()).slice(-2);
obj.timestamp = timestamp;
// 程序key
obj.app_key = config.AppKey;
// 参数数组
var arr = [];
// 循环添加参数项
for(var p in obj){
arr.push(p + obj[p]);
}
// 2、按首字母升序排列
arr.sort();
// 3、连接字符串
var msg = arr.join('');
// console.log(msg);
// 生成签名 sign hmac
var sign = md5(msg, config.AppSecret);
// 返回
return sign.toUpperCase();
}
module.exports.dySign = dySign;
拼接字符串并发送请求
var signGenerate = require('./create_dysign');
var http = require('http');
var qs = require('querystring');
var alidayuUrl = 'http://gw.api.taobao.com/router/rest';
// 短信发送的参数对象
var obj = {
format: 'json',
method: 'alibaba.aliqin.fc.sms.num.send',
v: '2.0',
timestamp: '2016-1-16 02:33:30',
partner_id: 'top-sdk-nodejs-20160116',
rec_num: '15110****', //手机号多个以逗号间隔
sign_method: 'hmac',
sms_type: 'normal',
sms_param: '{"code":"giscafer","product":"alidayu短信测试"}',
sms_free_sign_name: '身份验证',
sms_template_code: 'SMS_4725038'
}
//生成签名并拼接请求参数链接
var sign = signGenerate.dySign(obj);
console.log('签名:', sign);
obj.sign = sign;
obj.app_key = signGenerate.config.AppKey;
var arr = [];
for (var p in obj) {
arr.push(p + '=' + obj[p]);
}
var msg = arr.join('&')
var sendurl = alidayuUrl + '?' + msg;
console.log('完成请求地址:' + sendurl);
/**
* 短信发送请求测试
*/
var params = qs.stringify(obj);
var options = {
hostname: 'gw.api.taobao.com',
port: 80,
path: '/router/rest?' + params,
method: 'GET'
};
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
// console.log(req.path);
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('RESULT: ' + chunk);
});
});
req.on('error', function (e) {
console.log('ERROR: ' + e.message);
});
// write data to request body
req.write("执行完毕!");
req.end();
请求结果
{"alibaba_aliqin_fc_sms_num_send_response":{"result":{"err_code":"0","model":"100545278271^1100912293896","success":true},"request_id":"15q8um2g8w8ef"}}
博客原文http://giscafer.com/2016/01/16/alidayu-sign-nodejs/ alidayujs alidayujs