'auth/invalid-api-key' 当让 Admin SDK 发现服务帐户时
'auth/invalid-api-key' When Letting the Admin SDK discover a service account
在 Firebase Admin SDK 文档的不同页面中,例如 this page,建议:
If your code is deployed in an environment managed by Google, the
Admin SDK can attempt to auto-discover... the service account
provisioned for your app...To make use of these signing methods,
initialize the SDK with Google Application Default credentials and do
not specify a service account ID string: admin.initializeApp();
执行此操作时,我收到以下错误消息:
[Error: Your API key is invalid, please check you have copied it
correctly.] code: 'auth/invalid-api-key', message: 'Your API key
is invalid, please check you have copied it correctly.'
请注意,当我在我的项目中手动下载和导入凭据和服务帐户 JSON 文件时,我没有收到此错误消息。
重现错误的详细信息:
1- 我正在使用 Firebase CLI 在 Cloud Functions 上部署它。所以,基本上,我使用 firebase deploy
.
2- 这是我的 Node.js 应用程序中的最少代码:
const admin = require("firebase-admin");
const config = require("./firebase-config");
admin.initializeApp();
const firebase = require("firebase");
firebase.initializeApp(config);
const functions = require("firebase-functions");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
const cors = require("cors");
app.use(cors());
app.get("/", Some_Function);
exports.api = functions.https.onRequest(app);
当我用 firebase.initializeApp();
替换 firebase.initializeApp(config);
时发生错误
您正在尝试在服务器端环境中初始化 Firebase 客户端 SDK:
const firebase = require("firebase");
firebase.initializeApp(config);
与 firebase-admin
相比,客户端 SDK 接受一组完全不同的凭据。您可以在 GCP 托管环境(例如 Cloud Functions、Cloud 运行)中不带任何参数地初始化 firebase-admin
,但同样不适用于 firebase
。您需要提供从您的 Firebase 项目中获取的有效客户端应用配置。
const admin = require('firebase-admin');
admin.initializeApp(); // This is ok
const firebase = require('firebase');
firebase.initializeApp(); // This is wrong
有关如何获取客户端应用程序配置的详细信息,请参阅https://firebase.google.com/docs/web/setup#config-object。
另外请注意,在像 Functions 这样的环境中使用客户端 SDK 是相当不寻常的。我建议您重新考虑您的用例,看看您是否真的需要在您的函数中使用 firebase
客户端 SDK。
我试过了
dotenv
无效
process.env.PRIVATE_KEY.replace(/\n/g, '\n')
无效
第 1 步:转到控制台并生成新的服务帐户文件
第二步:export GOOGLE_APPLICATION_CREDENTIALS='path/to/serviceAccount.json'
(它在哪里并不重要,例如:'user/username/download/serviceAccount.json')
第 3 步:if(!admin.apps.length) admin.initializeApp();
In the docs, either export the json object as param or no param with step 2
在 Firebase Admin SDK 文档的不同页面中,例如 this page,建议:
If your code is deployed in an environment managed by Google, the Admin SDK can attempt to auto-discover... the service account provisioned for your app...To make use of these signing methods, initialize the SDK with Google Application Default credentials and do not specify a service account ID string:
admin.initializeApp();
执行此操作时,我收到以下错误消息:
[Error: Your API key is invalid, please check you have copied it correctly.] code: 'auth/invalid-api-key', message: 'Your API key is invalid, please check you have copied it correctly.'
请注意,当我在我的项目中手动下载和导入凭据和服务帐户 JSON 文件时,我没有收到此错误消息。
重现错误的详细信息:
1- 我正在使用 Firebase CLI 在 Cloud Functions 上部署它。所以,基本上,我使用 firebase deploy
.
2- 这是我的 Node.js 应用程序中的最少代码:
const admin = require("firebase-admin");
const config = require("./firebase-config");
admin.initializeApp();
const firebase = require("firebase");
firebase.initializeApp(config);
const functions = require("firebase-functions");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
const cors = require("cors");
app.use(cors());
app.get("/", Some_Function);
exports.api = functions.https.onRequest(app);
当我用 firebase.initializeApp();
firebase.initializeApp(config);
时发生错误
您正在尝试在服务器端环境中初始化 Firebase 客户端 SDK:
const firebase = require("firebase");
firebase.initializeApp(config);
与 firebase-admin
相比,客户端 SDK 接受一组完全不同的凭据。您可以在 GCP 托管环境(例如 Cloud Functions、Cloud 运行)中不带任何参数地初始化 firebase-admin
,但同样不适用于 firebase
。您需要提供从您的 Firebase 项目中获取的有效客户端应用配置。
const admin = require('firebase-admin');
admin.initializeApp(); // This is ok
const firebase = require('firebase');
firebase.initializeApp(); // This is wrong
有关如何获取客户端应用程序配置的详细信息,请参阅https://firebase.google.com/docs/web/setup#config-object。
另外请注意,在像 Functions 这样的环境中使用客户端 SDK 是相当不寻常的。我建议您重新考虑您的用例,看看您是否真的需要在您的函数中使用 firebase
客户端 SDK。
我试过了
dotenv
无效process.env.PRIVATE_KEY.replace(/\n/g, '\n')
无效
第 1 步:转到控制台并生成新的服务帐户文件
第二步:export GOOGLE_APPLICATION_CREDENTIALS='path/to/serviceAccount.json'
(它在哪里并不重要,例如:'user/username/download/serviceAccount.json')
第 3 步:if(!admin.apps.length) admin.initializeApp();
In the docs, either export the json object as param or no param with step 2