【发布时间】:2021-06-17 19:06:36
【问题描述】:
安装 Firebase CLI 并登录后,我可以创建这样的 JavaScript 文件,从 Firestore 检索数据并直接从本地命令行执行:
script.js:
const admin = require('firebase-admin');
(async () => {
admin.initializeApp({ projectId: 'my-project-id' });
const widget = await admin
.firestore()
.doc('widgets/someid')
.get();
console.log(widget.data());
})();
命令:node script.js
在我尝试从 Firebase 的身份验证中检索用户之前,它运行良好。然后我遇到了 403 错误。这是脚本:
const admin = require('firebase-admin');
(async () => {
admin.initializeApp({ projectId: 'my-project-id' });
const user = await admin
.auth()
.getUser('some-uid');
console.log(user);
})();
这是错误信息:
{
"error": {
"code": 403,
"message": "Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the identitytoolkit.googleapis.com.We recommend configuring the billing / quota_project setting in gcloud or using a service account through the auth / impersonate_service_account setting.For more information about service accounts and how to use them in your application, see https: //cloud.google.com/docs/authentication/.",
"errors": [{
"message": "Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the identitytoolkit.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.",
"domain": "usageLimits",
"reason": "accessNotConfigured",
"extendedHelp": "https://console.developers.google.com"
}],
"status": "PERMISSION_DENIED"
}
}
这很奇怪,因为我是以 GCP 项目的管理员身份登录的。上面的代码在云函数内部运行时也能很好地工作(它只有我所做的权限的一个子集)。如何更改权限和配置以使上述脚本正常工作?
【问题讨论】:
-
正如错误所说,您需要添加一个服务帐户才能从 Firebase 的身份验证中检索用户。在这个answer 中有几种方法可以实现它。
-
@Jordi 这很有趣。我正在使用 Firebase CLI,所以我认为我正在使用带有
firebase login的登录用户的凭据。该用户(我)可以完全访问包括 Firebase 身份验证在内的所有内容,但听起来我需要为我正在尝试做的事情创建一个特殊的服务帐户。
标签: firebase google-cloud-platform firebase-authentication firebase-cli