nodejs express 容联云通信 生成子帐号

安装中间件

cd 项目文件

npm install https

npm install MD5


代码:   /

/*   如果直接调用  请添加 返回函数callback  */

var express = require(‘express‘);

var router = express.Router();

var md5 = require(‘MD5‘); //MD5模块

var https = require(‘https‘); //https模块 用来发送异步请求(申请子账户)

function creatSid(callback){

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

                var accountSid = "accountSid";

                var key = "key";

                 //生成时间 因为new Date生成的时间戳与  容联云API接口要求的不同 所以自己组合

                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;

          //生成base64格式  Authorization 字符串

                  //base64(accountSid+":"+time)  time是之前组合成的时间                   

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

                    var s = b.toString(‘base64‘);

                    //生成加密字符串Sig

                    var SigParameter = md5(accountSid+key+time).toUpperCase();

                    var Authorization = s;

                    var phone = ‘18065741234‘;    //容联云子账户用户名(不能是中文)

                    //post数据包        

                    var data = {‘appId‘:‘aaf98f894c983f52014c9c0a4a3a0279‘,‘friendlyName‘:phone};

                    //转JSON字符串

                    data = JSON.stringify(data);

                    //POST  PATH路径(域名后路径)

                    var url = "/2013-12-26/Accounts/"+accountSid+"/SubAccounts?sig="+SigParameter;

                    //POST参数设置

                    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‘:data.length,

                            Authorization:Authorization 

                        }  

                    };  

                    var req = https.request(opt, function (serverFeedback) {         //建立POSTL链接

                            //res.send(serverFeedback.statusCode);

                            if (serverFeedback.statusCode == 200) {          

                                    var body = "";              

                                    serverFeedback.on(‘data‘, function (data) { body += data; }).on(‘end‘, function () { //获取返回数据

                                    body = JSON.parse(body);

                                    if(body.statusCode == 000000){

                                        return callback(body);

                                    }

                            }  

                            else {  

                                        return callback( "error");  

                             }  

                    });

                    req.write(data);          //发送  POST数据包

                    req.end();                //发送结束

}


路由器方法下调用

router.get(‘/‘, function(req, res, next) {

    creatSid(function(res){

        res.send(res);

    });    

});


module.exports = router;


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。