Nodejs Express Jong Communication Generation sub Account

Source: Internet
Author: User
Tags base64 sub account

Installing middleware

CD Project file

NPM Install HTTPS

NPM Install MD5


Code:/

/* If called directly, add the return function callback */

var express = require (' Express ');

var router = Express. Router ();

var MD5 = require (' MD5 '); //MD5 Module

var https = require (' https '); The//https module is used to send asynchronous requests (Request sub accounts)

function Creatsid (callback) {

var usermod = db.get (' user ');

var accountsid = "accountsid";

var key = "key";

Build time because the timestamp generated by the new date is different from the requirements of the Jong API interface, so you combine

var dates = new Date ();

var year = dates.getyear ()%100+2000;

var month = pad (dates.getmonth () +1,2);

var day = pad (Dates.getdate (), 2);

var hours = pad (dates.gethours (), 2);

var second = pad (Dates.getseconds (), 2);

var minute = pad (Dates.getminutes (), 2);

var time = year+ "" +month+ "" +day+hours+minute+second;

//Generate Base64 format Authorization string

Base64 (accountsid+ ":" +time) time is a combination of the previously

var b = new Buffer (accountsid+ ":" +time);

var s = b.tostring (' base64 ');

Generate an encrypted string sig

var sigparameter = MD5 (accountsid+key+time). toUpperCase ();

var Authorization = s;

var phone = ' 18065741234 '; Jong account username (not Chinese)

Post Data packet

var data = {' appId ': ' aaf98f894c983f52014c9c0a4a3a0279 ', ' FriendlyName ':p hone};

Go to JSON string

data = json.stringify (data);

Post path Path (after domain name)

var url = "/2013-12-26/accounts/" +accountsid+ "/subaccounts?sig=" +sigparameter;

Post parameter settings

var opt = {

method: "POST",

Host: "Sandboxapp.cloopen.com",

port:8883,

Path:url,

headers: {

' Accept ': ' Application/json ',

' content-type ': ' Application/json;charset=utf-8 ',

' content-length ':d ata.length,

authorization:authorization

}

};

var req = https.request (opt, function (serverfeedback) {//Establish POSTL link

//res.send (Serverfeedback.statuscode);

if (serverfeedback.statuscode = = 200) {

var body = "";

serverfeedback.on (' Data ', function (data) {body + = data;}). On (' End ', function () {//Get Return Data

BODY = json.parse (body);

if (Body.statuscode = = 000000) {

return callback (body);

}

}

Else {

return callback ( "error");

}

});

req.write (data); Send a post packet

req.end (); Send End

}


Called by the Router method

Router.get ('/', function (req, res, next) {

Creatsid (function (res) {

Res.send (RES);

});

});


Module.exports = router;


Nodejs Express Jong Communication Generation sub Account

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.