1, the description
Ali Big Fish provided a verification code, SMS notification, voice and other services, after the use of feeling very convenient, is worthy of Ali's products.
Recently want to get a nodejs to send SMS notification function, there is no use of alidayu-node (do not know this when you start doing.) If you use Alidayu-node, that's easy. ), the whole of their own signature sign, to achieve SMS delivery.
The official documentation describes the build steps for the signature sign, but the third step:
3, connection string connection parameter name and parameter value, and at the end plus secret
There is a mistake (plus secret is wrong)
2, Code implementation
Generate sign
/** * According to Taobao website provided by the tutorial * http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1/var MD5 =
Require ("BLUEIMP-MD5");
Taobao application app information (replaced by its own) var config = {appkey: ' 233002** ', Appsecret: ' 3403636b338e100399** '};
Exports.config=config;
var dysign = function (obj) {//generate timestamp 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; Program Key Obj.app_key = config.
Appkey;
Parameter array var arr = [];
Loops add parameter entries for (var p in obj) {Arr.push (P + obj[p]);
}//2, Arr.sort in ascending alphabetical order ();
3, connection string var msg = Arr.join (');
Console.log (msg); Generate signature sign HMAC var sign = MD5 (msg, CONFIG.
Appsecret);
Returns return Sign.touppercase (); } module.exports.dySign = DysigN
Stitching strings and sending requests
var signgenerate = require ('./create_dysign ');
var http = require (' http ');
var qs = require (' querystring ');
var alidayuurl = ' http://gw.api.taobao.com/router/rest '; SMS send Parameter object 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**** ',//mobile number multiple comma interval sign_method: ' HMAC ', Sms_typ E: ' Normal ', Sms_param: ' {' Code ': ' Giscafer ', ' product ': ' Alidayu SMS Test '} ', Sms_free_sign_name: ' Authentication ', Sms_template_code
: ' sms_4725038 '}//Generate signature and Splice request parameter link var sign = signgenerate.dysign (obj);
Console.log (' signature: ', 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 (' Completion request address: ' + sendurl ');
/** * SMS Send request Test/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 the body req.write (execution completed!)
");
Req.end ();
Request Results
Copy Code code as follows:
{"Alibaba_aliqin_fc_sms_num_send_response": {"result": {"Err_code": "0", "model": "100545278271^1100912293896", " Success ': true}, ' request_id ': ' 15q8um2g8w8ef '}}