node.js - 如何使用 Google Cloud 函数获取 URL?要求?

标签 node.js google-cloud-functions

我使用了 Google Apps Script

var response = UrlFetchApp.fetch(url, params);

从 api 获得响应。遗憾的是,我需要在云端硬盘中使用 Google Apps 脚本处理太多请求和太多数据

我现在的想法是切换到 Google 云功能和请求,但它不起作用。

const request = require('request');

const headers = {
    "Authorization" : "Basic " + Buffer.from('blabla').toString('base64')
};

const params = {
    "method":"GET",
    "headers":headers
};

exports.getCheckfrontBookings = (req, res) => {
  let url = 'https://fpronline.checkfront.com/api/3.0/item'

  request({url:url, qs:params}, function(err, response, body) {
    if(err) { console.log(err); return; }
    console.log("Get response: " + response.statusCode);
  });

最佳答案

request 原生支持回调接口(interface)但不返回 promise ,这是您必须在 Cloud Function 中执行的操作。

您可以使用 request-promise ( https://github.com/request/request-promise ) 和 rp(...) 方法,它“返回一个常规的 Promises/A+ 兼容 promise ”,然后做类似的事情:

const rp = require('request-promise');

exports.getCheckfrontBookings = (req, res) => {

  var options = {
    uri: 'https://fpronline.checkfront.com/api/3.0/item',
    headers: {
      Authorization: 'Basic ' + Buffer.from('blabla').toString('base64')
    },
    json: true // Automatically parses the JSON string in the response
  };

  rp(options)
    .then(response => {
      console.log('Get response: ' + response.statusCode);
      res.send('Success');
    })
    .catch(err => {
      // API call failed...
      res.status(500).send('Error': err);
    });
};

关于node.js - 如何使用 Google Cloud 函数获取 URL?要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54275148/

相关文章:

node.js - Node.js 中的模块和包之间的区别?

javascript - FS 文件观察器,获取更改

python-3.x - Google Cloud Functions 未返回响应

javascript - 无法部署 Firestore Cloud Function

ios - 如何在没有云功能的情况下将请求从 iOS (Swift) 发送到 Dialogflow V2 API?

javascript - 如何通过模块导出传递 promise 链中的最终解析变量?

angularjs - 如何创建基于 Node JS、MongoDB、Sails JS 的动态前端

javascript - 回调内变量的范围

firebase - 加速云功能

node.js - 将 SAML 与 JWT 用于 Node API