开发者社区> 问答> 正文

单文件实现读取accessToken和ticket

var https = require( "   ");//论坛设置不允许新人贴url
var querystring = require( 'querystring');
var events = require( 'events');

const OAPI_HOST = '开放平台网址';//论坛设置不允许新人贴url
const corpId = '你的corpID';
const secret = ‘你的corpSecret’;

var emitter = new events.EventEmitter();

function invoke(path, params) {
         https. get( OAPI_HOST + path + '?' + querystring.stringify(params), function(res) {
             if (res.statusCode === 200) {
                 var body = '';
                res. on( 'data', function (data) {
                     body += data;
                }). on( 'end', function () {
                     var result = JSON. parse( body);
                     if ( result && 0 === result.errcode) {
                         console. log( result);
                         if( result. access_token){
                             emitter. emit( 'atDone', result. access_token);
                        }
                         else{
                             if( result.ticket){
                                 emitter. emit( 'tkDone', result.ticket);
                            }
                        }
                    }
                     else {
                         console. log( result);
                    }
                });
            }
        })
}

invoke( '/gettoken', { corpid: corpId, corpsecret: secret});

emitter. on( 'atDone', function(accessTicket) {
     var ticket =   invoke( '/get_jsapi_ticket', { type: 'jsapi', access_token: accessTicket});
});
emitter. on( 'tkDone', function(ticket) {
     console. log( 'ticket rcvd');
});

求指导如何优雅的使用 [font=SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace]yield

展开
收起
elecfans 2017-08-01 19:18:55 2363 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载