Firebase功能:无法加载默认凭据 [英] Firebase Functions: Could not load default credentials

查看:62
本文介绍了Firebase功能:无法加载默认凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Firebase函数,该函数订阅一个Cloud PubSub主题.应用程序的初始化非常简单,如下所示:

I have a Firebase Function that subscribes to a Cloud PubSub topic. App is initialized very simply like this:

import * as admin from 'firebase-admin';

admin.initializeApp();

我收到此错误:

错误:无法加载默认凭据.浏览至 https://cloud.google.com/docs/authentication/getting-started 了解更多信息.在GoogleAuth.getApplicationDefaultAsync(/srv/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19)在process._tickCallback(internal/process/next_tick.js:68:7)"

"Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information. at GoogleAuth.getApplicationDefaultAsync (/srv/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19) at process._tickCallback (internal/process/next_tick.js:68:7)"

这很奇怪.通常可以正常工作.换句话说,如果我第二次触发它,它将起作用.还有第三次.大多数情况下,它似乎在新的 firebase部署之后首次运行失败,并且可能在冷启动"上运行.

Here's the weird thing. It typically works. In other words, if I trigger it a second time it works. And a third time. Most often it seems to fail the first time it runs after a new firebase deploy and possibly on a "cold start."

不确定我做错了什么,为什么它只会在第一次运行时失败.

Not sure what I'm doing wrong and why it would fail only on the first run.

推荐答案

已解决!这个答案有帮助:错误:无法加载默认凭据(用于Firestore的Firebase函数)

SOLVED! This answer helped: Error: Could not load the default credentials (Firebase function to firestore)

从Firebase函数中进行API调用,我正在发布到Cloud PubSub主题,如下所示:

From within a Firebase Function for an API call, I was publishing to a Cloud PubSub topic like this:

pubsub.topic(topicName).publish(dataBuffer, customAttributes)

我没有等待响应,而是立即将2XX HTTP响应发送回客户端.执行似乎继续正常,但显然它的行为不符合预期.

I was not awaiting the response and was immediately sending the 2XX HTTP response back to the client. The execution seemed to continue fine, but obviously it did not behave as intended.

有时,API响应调用本身将失败(并且永远不会发布消息),但有时不会失败.在其他情况下,发布将成功,但是订阅该主题的Firebase函数将失败!

Sometimes the API response call itself would fail (and never publish the message), but sometimes not. In other cases, the publish would succeed but the Firebase Function subscribing to the topic would fail!

在所有情况下,这似乎都可以在第二次运行脚本后自行解决.因此,我仍然认为这与冷启动有关.

In all cases, this seemed to resolve itself after running the script a second time. For this reason, I still believe it had something to do with a cold start.

但是由于我将其更改为等待:

But since I changed it to await like this:

await pubsub.topic(topicName).publish(dataBuffer, customAttributes)

我还没有看到这个问题再次发生.

I have not seen this problem happen again.

这篇关于Firebase功能:无法加载默认凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆