【问题标题】:TwiML app and Firebase throwing Unhandled promise rejection: Error: Could not load the default credentialsTwiML 应用程序和 Firebase 抛出未处理的承诺拒绝:错误:无法加载默认凭据
【发布时间】:2021-12-16 16:38:18
【问题描述】:

上下文:

我的应用通过 TwiML 使用 VoIP,我将通话记录和其他数据存储在 Firebase 中。创建呼叫时,Twilio 是创建/取消呼叫的中心点。因此,在拨打电话时,我会使用 Firebase 在 Firebase 中创建通话条目。


在线查找有关此问题、支持或类似问题的一些文档,但没有找到。问题很简单:

日志输出:

我看到这条消息的日志输出:

获取个人资料

然后得到这个错误:

UnhandledPromiseRejectionWarning:未处理的承诺拒绝:错误:无法加载默认凭据。浏览至https://cloud.google.com/docs/authentication/getting-started 了解更多信息。在 GoogleAuth.getApplicationDefaultAsync (/var/task/node_modules/google-auth-library/build/src/auth/googleauth.js:180:19) 在 processTicksAndRejections (internal/process/task_queues.js:97:5) 在异步 GoogleAuth .getClient (/var/task/node_modules/google-auth-library/build/src/auth/goo...


TwiML 应用代码 类似于 Firebase 函数

const AccessToken = require('twilio').jwt.AccessToken;
const VoiceGrant = AccessToken.VoiceGrant;
const VoiceResponse = require('twilio').twiml.VoiceResponse;

// Import the functions you need from the SDKs you need
const firebase = require("firebase-admin")

const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: "",
    measurementId: ""
};

// Initialize Firebase
console.log("initializing firebase")
const app = firebase.initializeApp(firebaseConfig);

exports.handler = async function(context, event, callback) {
    console.log("Running make-call")
    const from = event.From;
    let to = event.to;
    //...

    console.log("getting profile from")
    // exception is thrown at this line below
    var callerProfileSnap = await firebase.firestore(app).collection("profiles").doc(from).get();

    // originally had this, but attempted loading the app manually as "credentials" couldn't be loaded made me think this might be an issue
    // var callerProfileSnap = await firebase.firestore().collection("profiles").doc(from).get();

    // ... continuation of code

    var voiceResponse;
    callback(null, voiceResponse);
}

知道什么会导致这种情况/为什么会发生这种情况?在网上找不到太多解决方案

【问题讨论】:

    标签: javascript node.js firebase twilio twilio-twiml


    【解决方案1】:

    解决方案很简单,是由于疏忽造成的。

    Twilio 会自动在 event.From 字段前添加“client:”。这是我试图在 Firestore 中查找不存在的文档 client:abcd1234...

    然而,该错误具有误导性,与不正确的凭据无关,但实际上是缺少 Firestore 文档。


    可能的解决方案:

    1. 清理输入:

    检查event.From 是否包含“client:”,如果是,则调用.split(":")[1] 以获取ID 或From 参数的后半部分。

    1. 使用客户端发送的Twilio's custom parameters,它将包含在payload对象中。

    例如添加自定义参数,例如:

    customParams: {
        "hello": "world",
        "another": "entry"
    }
    

    然后将像这样在有效负载中可用:

    • payload.hello
    • payload.another

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 2021-02-15
      相关资源
      最近更新 更多