Rya*_*ers 4 javascript firebase google-cloud-functions
我正在尝试设置 GCP Cloud Function 来使用 生成电子邮件验证链接admin.auth().generateEmailVerificationLink
,但它会引发错误:
Error: 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.
Run Code Online (Sandbox Code Playgroud)
我能够使用以下云函数代码重现此错误:
索引.js:
const admin = require('firebase-admin');
admin.initializeApp();
exports.helloWorld = (req, res) => {
execute(res);
};
const execute = async (res) => {
const email = 'test@test.com';
const url = 'https://example.firebaseapp.com';
const link = await admin.auth().generateEmailVerificationLink(email, { url });
console.log(link);
res.status(200).send(link);
};
Run Code Online (Sandbox Code Playgroud)
包.json:
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"firebase-admin": "^10.0.2"
}
}
Run Code Online (Sandbox Code Playgroud)
我的 Firebase 管理服务帐户 ( firebase-adminsdk-XXX@example.iam.gserviceaccount.com
) 具有以下角色:
我还在 Firebase 控制台中查看了 API 密钥,在 GCP 中找到了它(Browser key (auto created by Firebase)
,并看到它选择了以下 API:
我尝试按照提供的链接(https://firebase.google.com/docs/admin/setupadmin
)进行操作,但它似乎特定于在GCP云功能之外进行设置(请参阅https://firebase.google.com/docs /admin/setup#initialize-without-parameters)。我还通读了 https://firebase.google.com/docs/auth/admin/email-action-links,但没有找到有用的详细信息。
我尝试使用functions.https.onCall
而不是常规的 GCP 导出。
我尝试设置FIREBASE_CONFIG={"projectId":"example","storageBucket":"example.appspot.com","locationId":"<my-region>"}
和GCLOUD_PROJECT=example
作为运行时环境变量。
问题是,因为我在 GCP 上部署该函数(而不是通过 Firebase),所以运行该函数的实际服务帐户不是 Firebase 控制台 ( firebase-adminsdk-XXX@example.iam.gserviceaccount.com
) 中指定的帐户,而是 App Engine 默认服务帐户:
在运行时,Cloud Functions 默认使用 App Engine 默认服务帐户 (PROJECT_ID@appspot.gserviceaccount.com)
来源: https ://cloud.google.com/functions/docs/concepts/iam#access_control_for_service_accounts
因此,在这种情况下,一旦我为我的帐户提供了example@appspot.gserviceaccount.com
角色,调用就起作用了:
顺便说一句,不需要额外的选项,admin.initializeApp()
也不需要运行时环境变量。