Firebase 可调用函数失败

Firebase callable functions fail

许多功能甚至在执行第一行代码之前就开始间歇性地失败并出现以下错误:

FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Failed to parse access token response: SyntaxError: Unexpected token p in JSON at position 4"."}

自从我打开 Firebase 支持的错误问题以来已经超过 5 五天了,但我仍然没有任何反馈。

有人知道为什么会这样吗?如何解决?

使用的库及其版本:

"firebase": "^8.2.9",
"firebase-admin": "^9.5.0",
"firebase-functions": "^3.1.0"

初始化:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

var config = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "..."
};
var firebase = require("firebase");
firebase.initializeApp(config);

在 Cloud Function 中,如果您想与 Firebase 服务(例如 Firestore、Auth 服务等)进行交互,则需要使用 Admin SDK.

因此,您需要加载 firebase-functionsfirebase-admin 模块,并初始化一个 admin 您与服务交互的应用程序实例,如下所示:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

const firestoreDB = admin.firestore();
const authService = admin.auth();

// ... 

// Examples:

// In a Cloud Function
return firestoreDB.collection("cities").doc("LA").set({
    name: "Los Angeles",
    state: "CA",
    country: "USA"
});


// In another Cloud Function
return authService.updateUser(uid, {
   email: 'modifiedUser@example.com',
   phoneNumber: '+11234567890',
})
.then((userRecord) => {
  // ...
  // return ...
})

换句话说,你不需要做:

var config = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "..."
};
var firebase = require("firebase");
firebase.initializeApp(config);

另请注意 doc 中的这条注释:

In many cases, new features and bug fixes are available only with the latest version of the Firebase CLI and the firebase-functions SDK. It's a good practice to frequently update both the Firebase CLI and the SDK with these commands inside the functions folder of your Firebase project:

npm install firebase-functions@latest firebase-admin@latest --save

npm install -g firebase-tools