码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-04-12 00:09:26      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

安装中间件

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;


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

标签:

原文地址:http://my.oschina.net/u/2313904/blog/399078

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!