http://blog.csdn.net/baby97/article/details/49702379
微信的接口都使用HTTPS协议,使用NodeJS需创建HTTPS对象.
详细使用参考官网文档https://nodejs.org/api/https.html
代码如下:
- var https = require("https");
- var iconv = require("iconv-lite");
- var url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
- https.get(url, function (res) {
- var datas = [];
- var size = 0;
- res.on('data', function (data) {
- datas.push(data);
- size += data.length;
- //process.stdout.write(data);
- });
- res.on("end", function () {
- var buff = Buffer.concat(datas, size);
- var result = iconv.decode(buff, "utf8");//转码//var result = buff.toString();//不需要转编码,直接tostring
- console.log(result);
- });
- }).on("error", function (err) {
- Logger.error(err.stack)
- callback.apply(null);
- });