【问题标题】:Is it possible to call firebase functions with 'firebase-admin' package?是否可以使用“firebase-admin”包调用 firebase 函数?
【发布时间】:2020-11-30 00:18:11
【问题描述】:

我有基于服务帐户访问的节点应用程序,所以我使用了firebase-admin。正如我之前看到的,firebase-admin 大部分重复了firebase 包(除了身份验证部分、签名和其他一些部分),但现在我想调用函数,但我找不到firebase.apps[0].functions().httpsCallable('myFunction') 的任何等效项。我查看了 Typescript 的类型,他们甚至没有提到函数。

admin.initializeApp({
  credential: admin.credential.cert('./service-account-credentials.json'),
  databaseURL: process.env.REACT_APP_FIREBASE_DATABASE_URL,
});

const config = {
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
};

const storageBucket = admin.storage().bucket(config.storageBucket);
const firestore = admin.firestore();



// const functions = admin.apps[0].functions(); // not possible
const functions = firebase.apps[0].functions(); // possible, but Firestore.apps not initialized

我有什么选择?

【问题讨论】:

    标签: node.js firebase firebase-admin


    【解决方案1】:

    Node.js Admin SDK for Firebase 中没有用于调用 Callable Cloud Functions 的 firebase-functionsclient

    虽然这不是一个不合理的要求,所以我建议在Github repo 上提出功能请求,这样其他人也可以参与进来。

    与此同时,唯一的选择是自己实现wire protocol for callable Cloud Functions,可能基于他们的implementation on the regular JavaScript SDK

    【讨论】:

    • 感谢您的回答,期待这样的事情。实际上是在考虑使用 firebase 用户的电子邮件和密码为“firebase”库初始化另一个应用程序。
    • 我来到这里是为了寻找一种在我运行 Nodejs 的 Google Cloud IoT 设备上使用 httpsCallable 的方法。我需要调用一个云函数,请求 Mender 在他们的平台上预授权设备。设备首次启动时从 systemd 服务。 @DougStevens 提到使用我拥有的服务帐户here,但听起来这并不能解决我的问题。有什么新的发展或建议的变通办法?
    • 我的回答中提到的解决方法仍然是我所知道的唯一一种。
    • 我尝试使用 web sdk 运行,因为它在这里指出它适用于“运行 Node.js 的物联网设备”。 here。但它会抛出; menderPreAuth Error: Error: internal (node:773) UnhandledPromiseRejectionWarning: Error: internal at new HttpsErrorImpl (/data/node_modules/.pnpm/@firebase/functions@0.6.1_e9d66a23fa701e65014ff8d410061da5/node_modules/@firebase/functions/dist/index.node.cjs.js:62:28) 什么是“错误:内部”?
    • 抱歉,这不是一个帮助解决问题的好地方。如果您在应用中实现 Cloud Functions 时遇到问题,请使用自己的 MCVE 发布一个新问题。
    【解决方案2】:

    结束了这段代码。用户 id 是 firebase 用户 uid。

    const firebaseApp = async (userId: string) => {
      if (firebase.apps?.length) {
        firebase.apps.forEach((app) => app.delete());
        console.log(`Deleted ${firebase.apps.length} apps`);
      }
    
      const customToken = await admin.auth().createCustomToken(userId);
      const firebaseApp = firebase.initializeApp(config);
    
      await firebaseApp.auth().signInWithCustomToken(customToken);
    
      return firebaseApp;
    };
    
    const firebase = await firebaseApp('WObPvJbZDIR9WB72Tun8Jg4D0ky2');
    
    await firebase
      .functions('europe-west1')
      .httpsCallable('myFunction')(doc);
    

    【讨论】:

      猜你喜欢
      • 2020-05-17
      • 2018-06-17
      • 1970-01-01
      • 2018-10-21
      • 2020-04-18
      • 2020-04-03
      • 2021-03-12
      • 1970-01-01
      • 2021-08-09
      相关资源
      最近更新 更多