简体   繁体   English

Firebase Admin INVALID_APP_OPTIONS 错误在 initializeApp()

[英]Firebase Admin INVALID_APP_OPTIONS error at initializeApp()

I'm trying to connect Instagram OAuth to Firebase through Node.js back-end.我正在尝试通过 Node.js 后端将 Instagram OAuth 连接到 Firebase。 I have successfully retrieved Instagram account data including access_token which I want to exchange with firebase-admin 's createCustomToken on my Node.js backend.我已成功检索到 Instagram 帐户数据,包括我想与我的 Node.js 后端上的 firebase firebase-admincreateCustomToken交换的access_token My objective here is to generate custom token so my Angular app can do signInWithCustomToken(token) into my Firebase app.我的目标是生成自定义令牌,以便我的 Angular 应用程序可以在我的 Firebase 应用程序中执行signInWithCustomToken(token) There is no problem on retrieving data from Instagram as I can print my JSON object on console.从 Instagram 检索数据没有问题,因为我可以在控制台上打印我的 JSON object。

The problem is occurred when I want to exchange my access_token to Firebase Custom Token.当我想将我的access_token交换为 Firebase 自定义令牌时出现问题。

I have followed this guide from Firebase Admin page for Node.js and I'm facing an error message below我已按照 Node.js 的 Firebase 管理页面中的本指南,我在下面遇到错误消息

throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_OPTIONS, "Invalid Firebase app options passed as the first argument to initializeApp() for the " + throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_OPTIONS, "无效的 Firebase 应用程序选项作为第一个参数传递给 " +

Error: Invalid Firebase app options passed as the first argument to initializeApp() for the app named "[DEFAULT]".错误:无效的 Firebase 应用程序选项作为名为“[DEFAULT]”的应用程序的 initializeApp() 的第一个参数传递。 The "credential" property must be an object which implements the Credential interface. “凭据”属性必须是实现凭据接口的 object。

Here is my code on related issue.这是我关于相关问题的代码。

 // authService.js var fbAdmin = require('firebase-admin'); var serviceAccount = require('./key/key.json'); function createFirebaseToken(instagramID) { // I copy & pasted this var from other class var config = { apiKey: "MY_FIREBASE_APIKEY", authDomain: "MY_APP.firebaseapp.com", databaseURL: "https://MY_APP.firebaseio.com", storageBucket: "MY_APP.appspot.com", }; console.log(fbAdmin.credential.cert(serviceAccount)); // serviceAccount successfully printed on console // Error appears when executing this function fbAdmin.initializeApp({ serviceAccount: fbAdmin.credential.cert(serviceAccount), databaseURL: config.databaseURL }); const uid = `instagram:${instagramID}`; // Create the custom token. console.log(uid); return fbAdmin.auth().createCustomToken(uid); }

It appears that my Node app cannot initialize a connection to firebase-admin but I have no idea the solution as I am a beginner on these technologies.我的 Node 应用程序似乎无法初始化与firebase-admin的连接,但我不知道解决方案,因为我是这些技术的初学者。 Please advice.请指教。

This issue can be resolved by updating firebase-tools, firebase-functions and firebase-admin packages to the latest version.此问题可以通过将 firebase-tools、firebase-functions 和 firebase-admin 软件包更新到最新版本来解决。

 npm install -g firebase-tools@latest npm install firebase-functions@latest npm install firebase-admin@latest

Refer this GitHub page for more details.有关详细信息,请参阅此GitHub 页面

Just stumbled upon on Firebase Admin Release Notes at version 5.0.0 on May 2017 stated that serviceAccount has been removed.刚刚在 2017 年 5 月的 5.0.0 版中偶然发现Firebase 管理员发行说明指出serviceAccount已被删除。 So instead of forcing to use serviceAccount , I use credential instead.因此,我没有强制使用serviceAccount ,而是使用credential

 fbAdmin.initializeApp({ credential: fbAdmin.credential.cert({ projectId: '<APP_ID>', clientEmail: "foo@<APP_ID>.iam.gserviceaccount.com", privateKey: "-----BEGIN PRIVATE KEY-----\n<MY_PRIVATE_KEY>\n-----END PRIVATE KEY-----\n" }), databaseURL: config.databaseURL });

Updated Answer更新的答案

After continuing to work with this, it seems to be able to call admin.initializeApp() without any params, you need to make sure your `/functions nodeJs setup is up to date.在继续使用这个之后,它似乎能够在没有任何参数的情况下调用admin.initializeApp() ,你需要确保你的`/functions nodeJs 设置是最新的。

In /functions directory, run this command:/functions目录中,运行以下命令:

npm i --save firebase-functions@latest

Thanks to: https://stackoverflow.com/a/49437619/2162226感谢: https://stackoverflow.com/a/49437619/2162226

.. I am still getting familiar with all the nodeJS/npm details - not sure why, but seems I had to run it a few times for everything to get into place. .. 我仍然熟悉所有 nodeJS/npm 细节 - 不知道为什么,但似乎我必须运行它几次才能让一切到位。 That and the sudo npm install -g firebase-tools updated as well.那和sudo npm install -g firebase-tools也更新了。

According to official docs, we need to be using at least version Firebase 4.12.0 in order to get the latest cloud functions support: https://firebase.google.com/docs/functions/callable根据官方文档,我们需要使用至少版本 Firebase 4.12.0 以获得最新的云功能支持: https://firebase.google.com/docs/functions/callable

First Answer第一个答案

I ran into the same error message in trying to set up Cloud Functions for Firebase.我在尝试为 Firebase 设置 Cloud Functions 时遇到了相同的错误消息。

Passing in the argument for admin.initializeApp solved it for my project: admin.initializeApp的参数为我的项目解决了这个问题:

 const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase);

After a good amount of search, gratefully found this thread with a post where it has that line of code: https://github.com/firebase/firebase-functions/issues/99 .. because so far haven't seen it anywhere in the docs about that parameter经过大量搜索后,感激地发现这个帖子有一个帖子,它有那行代码: https://github.com/firebase/firebase-functions/issues/99 ..因为到目前为止还没有在任何地方看到它在有关该参数的文档中

According to the release docs for Jan. 11, 2018:根据 2018 年 1 月 11 日的发布文档:

https://firebase.google.com/support/release-notes/admin/nodehttps://firebase.google.com/support/release-notes/admin/node

The admin.initializeApp() method can now be invoked without any arguments.现在可以在没有任何 arguments 的情况下调用 admin.initializeApp() 方法。 This initializes an app using Google Application Default Credentials, and other AppOptions loaded from the FIREBASE_CONFIG environment variable.这将使用 Google 应用程序默认凭据和从 FIREBASE_CONFIG 环境变量加载的其他 AppOptions 初始化应用程序。

Kindly use the following snippet, which is mentioned in the Firebase Admin SDK for Node.js.请使用 Firebase Admin SDK for Node.js 中提到的以下代码段。 Download the JSON file from the Service Account in the Project Settings and change the path in serviceAccount .从 Project Settings 中的 Service Account 下载 JSON 文件并更改serviceAccount中的路径。 Also add your databaseUrl , which is of the form http://app_name/.firebaseio.com还要添加您的databaseUrl ,其格式为 http://app_name/.firebaseio.com

 var admin = require("firebase-admin"); var serviceAccount = require("path/to/serviceAccountKey.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "yourDatabaseUrl" });

Check what version of firebase-admin module you are using.检查您使用的 firebase-admin 模块的版本。 As per the release note of version 5.9.1 The admin.initializeApp() method can now be invoked without a credential option. The SDK uses Google Application Default Credentials when initialized this way.根据版本 5.9.1 的发行说明, The admin.initializeApp() method can now be invoked without a credential option. The SDK uses Google Application Default Credentials when initialized this way. The admin.initializeApp() method can now be invoked without a credential option. The SDK uses Google Application Default Credentials when initialized this way.

release notes发行说明

For those having this issue with "firebase-admin": "^11.0.0" , you have to construct the certification passed into initializeApp() using cert imported from firebase-admin/app .对于那些遇到"firebase-admin": "^11.0.0"问题的人,您必须使用从firebase-admin/app导入的证书构建传递给initializeApp()cert

Example:例子:

 import { initializeApp, cert } from "firebase-admin/app"; const admin = initializeApp({ credential: cert({ projectId: process.env.MY_PROJECT_ID, clientEmail: process.env.MY_PRIVATE_KEY, privateKey: process.env.MY_CLIENT_EMAIL, }), });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用管理员权限在 firebase 中初始化应用程序 - how to initializeApp in firebase with admin privilege 在Firebase Cloud函数中调用admin.initializeApp()结束时是否需要调用app.delete() - Is it neccessary to call app.delete() at the end of calling admin.initializeApp() in firebase cloud function Firebase Admin SDK-错误:提供了用于初始化App()的凭据实现…无法获取有效的Google OAuth2访问令牌 - Firebase Admin SDK - Error: Credential implementation provided to initializeApp() … failed to fetch a valid Google OAuth2 access token K8S 集群中的 Google FCM firebase-admin initializeApp() 错误 - Google FCM firebase-admin initializeApp() error in K8S cluster 错误:没有 Firebase 应用程序“[DEFAULT]”已创建 - 调用 Firebase App.initializeApp() - Error: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() 将 firebase 添加到应用程序和服务器时初始化应用程序 - initializeApp when adding firebase to app and to server TypeError:无法将 object 转换为 firebase-admin.initializeApp() 中的原始值 - TypeError: Cannot convert object to primitive value in firebase-admin.initializeApp() 无法导出 firebase-admin 的 initializeApp function - Cannot export firebase-admin's initializeApp function Cloud Functions Shell 中的 initializeApp() 上的 Firebase 无效凭据 302 - Firebase invalid-credential 302 on initializeApp() in Cloud Functions Shell Firebase 管理员令牌无效 - Firebase Admin Invalid Token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM