Firebase Admin Node.js创建自定义令牌报错:IAM服务账号凭据API未启用
问题场景
使用Node.js的Firebase Admin SDK创建自定义令牌时遇到报错,代码如下:
admin.auth() .createCustomToken(uid) .then((customToken) => { // 业务处理逻辑 }) .catch((error) => { // 错误捕获逻辑 });
报错提示:
IAM Service Account Credentials API has not been used in project before or it is disabled. Enable it by visiting the relevant page then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.; Please refer to the Firebase documentation for more details on how to use and troubleshoot this feature.
已在Google Cloud控制台启用对应API,但数小时后仍出现相同错误。
可行解决方案
确认API启用的项目匹配
检查Google Cloud控制台中启用IAM Service Account Credentials API的项目,是否与当前Firebase Admin SDK绑定的项目完全一致。误操作到其他项目是常见的无效启用原因。配置服务账号必要权限
确保Firebase Admin使用的服务账号拥有Service Account Token Creator角色:- 进入Google Cloud控制台「IAM与管理员」→「IAM」页面
- 找到对应服务账号(通常命名为firebase-adminsdk开头)
- 点击编辑按钮,添加「Service Account Token Creator」角色,保存后等待权限同步生效(一般需要数分钟)
重新初始化Admin SDK并重启服务
确认服务账号密钥文件对应正确项目,然后重启Node.js服务,确保SDK加载最新的配置与权限信息。触发API状态同步
进入Google Cloud控制台的IAM Service Account Credentials API页面,确认状态为「已启用」,可通过控制台的「测试」功能手动调用一次API,强制触发系统同步状态,解决自动同步延迟问题。排查账单与限制情况
检查项目是否存在欠费导致API被限制,若有欠费需及时补缴;同时确认项目未达到API调用配额上限。
内容的提问来源于stack exchange,提问作者Amanuel

