Firebase 函数 运行 在本地未使用 Firebase Admin SDK 进行身份验证

Firebase Functions running locally not authenticating with Firebase Admin SDK

当部署到 Firebase 项目时,我能够成功运行此代码。

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

admin.initializeApp();

exports.createUser = functions.https.onRequest((req, res) => {
  admin.auth().createUser({ uid: req.body.id })
    .then(user => {
      return res.send(user);
    })
    .catch(err => {
      return res.status(422).send({ error: err });
    });
});

这是来自 Firebase 的响应:

{
    "uid": "abcdefg1234",
    "emailVerified": false,
    "disabled": false,
    "metadata": {
        "lastSignInTime": null,
        "creationTime": "Sun, 20 May 2018 22:22:55 GMT"
    },
    "tokensValidAfterTime": "Sun, 20 May 2018 22:22:55 GMT",
    "providerData": []
}

但是,当我 运行 函数 在本地 firebase serve 并调用相同的函数(使用 Postman)时,我 运行 进入权限错误:

{
    "error": {
        "code": "auth/insufficient-permission",
        "message": "Credential implementation provided to initializeApp() via the \"credential\" property has insufficient permission to access the requested resource. See https://firebase.google.com/docs/admin/setup for details on how to authenticate this SDK with appropriate permissions."
    }
}

本地 运行ning Firebase Functions 实例上的 firebase-admin 身份验证设置是否不同于 运行ning 正常生产 Firebase 服务器上的身份验证设置?

规格:

 "dependencies": {
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  }

经过进一步审查:

https://firebase.google.com/docs/functions/local-emulator

If you want your functions tests to interact with Google APIs or other Firebase APIs via the Firebase Admin SDK, you may need to set up admin credentials

Cloud Firestore and Realtime Database triggers already have sufficient credentials, and do not require additional setup.

All other APIs, including Firebase APIs such as Authentication and FCM or Google APIs such as Cloud Translation or Cloud Speech, require the setup steps described in this section. This applies whether you're using the functions shell or firebase serve.

看来问题的答案是 "yes",您可能需要不同的身份验证过程,具体取决于您访问的 Firebase 服务。