【问题标题】:Calling Azure API from Google Cloud Function从 Google Cloud Function 调用 Azure API
【发布时间】:2018-08-13 18:32:39
【问题描述】:

我开发了谷歌云函数,它调用托管在 AZURE 中的 API。 但是函数返回错误

错误:函数崩溃。详情: getaddrinfo ENOTFOUND https://bupanonproduction.azure-api.net https://bupanonproduction.azure-api.net:443

下面是谷歌云功能

'use strict';
const http = require('https');
const host = 'https://bupanonproduction.azure-api.net';
exports.remaininglimits = (req, res) => {
  // Call the API
  callRemainingLimitsApi().then((output) => {
    // Return the results from the API to API.AI
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': output, 'displayText': output }));
  }).catch((error) => {     
        // If there is an error let the user know
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': error, 'displayText': error }));
  });
};
function callRemainingLimitsApi () {
  return new Promise((resolve, reject) => {
    // Create the path for the HTTP request to get the weather
    let path = '/api/Values';
    console.log('API Request: ' + host + path);
    // Make the HTTP request to get the weather
    http.get({host: host, path: path, headers: {'Ocp-Apim-Subscription-Key':'0a6e2fa822ec4d7a821d7f286abb6990'}}, (res) => {
         let body = ''; // var to store the response chunks
      res.on('data', (d) => { body += d; }); // store each response chunk
      res.on('end', () => {
        // After all the data has been received parse the JSON for desired data
        let response = JSON.parse(body);
        let jasonString = JSON.stringify(response);
        // Create response
        let output = `Hi, your limit is ${jasonString}.`;
        // Resolve the promise with the output text
        console.log(output);
        resolve(output);
      });
      res.on('error', (error) => {
        reject(error);
      });
    });
  });
}

当我使用下面的其他公共 API 时,它会向云函​​数返回正确的结果。

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo

知道为什么云函数无法识别 AZURE API 网址吗?

-艾伦-

【问题讨论】:

标签: javascript google-cloud-platform google-cloud-functions dialogflow-es azure-api-management


【解决方案1】:

我刚刚发现应该在没有前缀“https”的情况下定义主机。这解决了问题。我正在使用 300 美元信用额度的免费试用版,但我不确定这是否被视为付费计划。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-22
    • 2017-12-17
    • 2020-06-19
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多