尝试调用Google Places(照片)API时,GoogleCloudFunction抛出FetchError错误

时间:2019-07-16 23:03:03

标签: node.js firebase google-cloud-functions google-places-api

每当将文档添加到Cloud Firestore数据库时,我就使用Cloud Functions for Firebase触发API调用。我基本上只是在抓取新创建的文档的photoReference值,并尝试获取google place photo api的URL值(而不是照片本身)并将其存储在新字段imageUrl中。

我隔离了该问题,当我在本地触发云功能时,它可以工作,但是当我部署它时,它会向我返回读取错误(见下文)。

exports.googlePlaceImageApiTest2 = functions.https.onRequest(async (req, res) => {
  // target i want to receive: "https://lh3.googleusercontent.com/p/AF1QipPIseBGCsj9tkRu3LLmjLwWMR7ARrVekgZp8rQ8";
  // helper method
  const handleErrors = response => {
    if (!response.ok) {
      throw Error(response.statusText);
    }
    return response;
  };
  // START API CALL
  console.log("starting API call");
  const apiKey = "xxxxxx";
  const photoReference = "CmRaAAAAEEo1d6LnuIvTCKlxy_NbyJ48Mg0dIzoRhGQ_J7NgbVGyxecEWMFJqdOjSQxpTATBebjflJyvSJab787G54kyVsnBF1b73IMLJSROH50KMpzMGZC1hzHNxzszlm3D6Ow5EhCivI6d91eRP3--M7qYbg8hGhQzVqmiTXHqKN7IqzYnQedaILWJpQ";
  const url = `https://maps.googleapis.com/maps/api/place/photo?key=${apiKey}&photoreference=${photoReference}&maxwidth=400`;

  let fetch = require("node-fetch");
  fetch(url)
  .then(handleErrors)
  .then(response => {
    const target = response.url;
    console.log(">>>>>FINAL URL: ", target);
    res.status(200).send(target).end();
    return 0;
  }).catch(error => {
    console.log(">>>>>ERROR!!!");
    console.log(error);
    res.status(500).send("error").end();
    return 0;
  });
});

这就是我在Firebase控制台中检查日志时得到的内容

{ FetchError: request to https://lh3.googleusercontent.com/p/AF1QipPIseBGCsj9tkRu3LLmjLwWMR7ARrVekgZp8rQ8=s1600-w400 failed, reason: getaddrinfo EAI_AGAIN lh3.googleusercontent.com:443
    at ClientRequest.<anonymous> (/srv/node_modules/node-fetch/lib/index.js:1455:11)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:401:9)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:66:8)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)
  message: 'request to https://lh3.googleusercontent.com/p/AF1QipPIseBGCsj9tkRu3LLmjLwWMR7ARrVekgZp8rQ8=s1600-w400 failed, reason: getaddrinfo EAI_AGAIN lh3.googleusercontent.com:443',
  type: 'system',
  errno: 'EAI_AGAIN',
  code: 'EAI_AGAIN' }

0 个答案:

没有答案