javascript - 如何在 promise 中包装 google oauth 授权回调?

标签 javascript node.js rest google-apps-script promise

我已经学习了 google nodejs 快速入门之一 examples作为如何进行 oauth 的模型,将其重写为类对象,以便我可以将其包含在我的大型项目中。我可以成功运行应用程序脚本 API 调用 scripts.run并在类对象中获取有效的返回值,但不知道如何将其返回到包含项目。

包含函数的 scripts.run 看起来像这样

  Goo.prototype.testAuth = function( auth ){
     var script = google.script('v1');
     var cmd = {
        auth,
        resource: { function: 'test_auth', devMode: true },
        scriptId: '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
     };

     script.scripts.run(
        cmd,
        function( err, resp ){
        if( err ){ console.log('The API returned an error: ' + err);return }
        if( resp.error ){console.log('Script error message: '
                             + resp.error.details[0].error.errorMessage);}
        else {
           console.log('resp from api call', resp.response.result );
           return resp.response.result ;
        }
     });

  };

返回resp.response.result 是有问题的部分,因为它没有传回容器中的 var response

 var Goo = require('./Goo.js');
 var goo = new Goo();
 var response = goo.test_auth();
 console.log('response to use elsewhere ',response);

熟悉的人都知道,Goo类中的console.log返回一个值,而容器中的console.log返回undefined。

如果重要的话,所有的 Goo 类都像这样包装

  (function(){
     var Goo = (function(){
        var Goo = function(config){
        }
        Goo.prototype.test_auth = function(){
            this.authorize( this.testAuth );
        };
        Goo.prototype.testAuth = function(){};
        Goo.prototype.authorize = function(){};
     })();
     module.exports = Goo;
  })();

我应该如何构造它以返回要在容器中使用的值?

我不清楚我是否应该尝试将 script.scripts.run 包装在一个 promise 中,如果它已经返回一个 promise 而我不知道如何等待它返回,或者我是否“m 处理回调函数使其成为错误的解决方案。感谢此处的任何指导。

我正在使用 node.js 和 googleapis ^24.0.0

最佳答案

testAuth 函数不会返回当前写入的任何内容。可以像下面这样“ promise ”它......

Goo.prototype.testAuth = function( auth ){
    return new Promise((resolve, reject) => {
        var script = google.script('v1');
        var cmd = {
           auth,
           resource: { function: 'test_auth', devMode: true },
           scriptId: '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
        };

        script.scripts.run(
           cmd,
           function( err, resp ){
           if( err ){
               console.log('The API returned an error: ' + err);
               reject(err);
           } else if( resp.error ){
               console.log('Script error message: ' + resp.error.details[0].error.errorMessage);
               reject(resp.error);
           } else {
               console.log('resp from api call', resp.response.result );
               resolve(resp.response.result);
           }
        });
    });
};

然后这样调用...

var Goo = require('./Goo.js');
var goo = new Goo();
goo.test_auth().then(response => {
    console.log('response to use elsewhere ',response);
}).catch(error => {
    console.log('error ',error);
});

抱歉,缩进格式有点奇怪。 OP 使用 3 个空格来缩进,而不是通常的 2 或 4 个。( self 注意:下一集“硅谷”的想法)

关于javascript - 如何在 promise 中包装 google oauth 授权回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50307860/

相关文章:

javascript - 闭包编译 ADVANCED_OPTIMIZATIONS 提示使用此

javascript - Vue 组合 API : Is there a better way in calling `emit` inside consumable file?

node.js - 蒙戈错误: failed to connect to server [<docker-machine>:<port>] on first connect

java - Spring 4 + Jersey 集成@Autowired

python - Django json序列化不返回对象

c# - 使用 RestSharp 反序列化 JSON

javascript - 匹配所有值,替换为 OR 运算符

javascript - 如何在 node-resque 中重用连接?

javascript - 在 Node 中将循环转换为异步

node.js - 如何删除 npm 包