如何将 Firebase 管理员连接到模拟器身份验证

发布于 2025-01-10 01:33:23 字数 2089 浏览 0 评论 0原文

我正在努力通过 Firebase Admin SDK 连接到模拟的 Firebase Auth 服务。我削减了代码以真正使问题突出,并希望有人能提供帮助。

这是我运行的 test.js 的代码(在 NodeJS 中):

// Someone said these two lines should allow the firebase-admin
// SDK to connect to the emulators, but... no.
process.env['GCLOUD_PROJECT'] = 'my-firebase-project-id'
process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080'
const admin = require('firebase-admin')
const app = admin.initializeApp()
const auth = app.auth()
console.log('I have an auth service object')
auth.listUsers().then(users => console.log(users))

我像这样运行模拟器:

firebase emulators:start --only auth

当我运行 test.js 文件时,我得到以下结果:

PS C:\...\functions> node .\test.js
I have an auth service object
(node:18232) UnhandledPromiseRejectionWarning: Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo EAI_AGAIN metadata.google.internal. Error code: EAI_AGAIN".
    at FirebaseAppError.FirebaseError [as constructor] (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:44:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:90:28)
    at new FirebaseAppError (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:125:28)    
    at C:\...\functions\node_modules\firebase-admin\lib\app\firebase-app.js:87:19
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

我在 Windows 上使用以下版本的 firebase 运行此程序:

    "firebase-admin":     "^10.0.2",
    "firebase-functions": "^3.18.1",

我读到了有关获取秘密凭据密钥并添加其路径的信息,如下所示:

process.env['GOOGLE_APPLICATION_CREDENTIALS'] = 'C:\\...\\functions\\.runtimekey.json'

并且只要我可以访问真实的云身份验证实例,它就可以“工作”(只要模拟器已关闭)但这不是我想要什么。我想连接 firebase-admin 并获取模拟 Auth 实例中的用户列表。

非常感谢您提供的任何帮助!

I am struggling to connect to the emulated Firebase Auth service via the Firebase Admin SDK. I cut down the code to really make the problem stand out, and hope someone can help.

This is the code of the test.js I run (in NodeJS):

// Someone said these two lines should allow the firebase-admin
// SDK to connect to the emulators, but... no.
process.env['GCLOUD_PROJECT'] = 'my-firebase-project-id'
process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080'
const admin = require('firebase-admin')
const app = admin.initializeApp()
const auth = app.auth()
console.log('I have an auth service object')
auth.listUsers().then(users => console.log(users))

I run the emulators like this:

firebase emulators:start --only auth

When I run the test.js file, I get this:

PS C:\...\functions> node .\test.js
I have an auth service object
(node:18232) UnhandledPromiseRejectionWarning: Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo EAI_AGAIN metadata.google.internal. Error code: EAI_AGAIN".
    at FirebaseAppError.FirebaseError [as constructor] (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:44:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:90:28)
    at new FirebaseAppError (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:125:28)    
    at C:\...\functions\node_modules\firebase-admin\lib\app\firebase-app.js:87:19
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

I run this on Windows with the following versions of firebase:

    "firebase-admin":     "^10.0.2",
    "firebase-functions": "^3.18.1",

I read about getting a secret credentials key and adding its path like this:

process.env['GOOGLE_APPLICATION_CREDENTIALS'] = 'C:\\...\\functions\\.runtimekey.json'

And that 'works' in as much as I then can access the real cloud auth instance (as long as the emulators is off) but that isn't what I want. I want to connect firebase-admin and get a list of users in the emulated Auth instance.

Many thanks for any help you can offer!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九厘米的零° 2025-01-17 01:33:24

设置环境变量 FIREBASE_AUTH_EMULATOR_HOST

export FIREBASE_AUTH_EMULATOR_HOST=localhost:9099

不要包含协议方案(即 http/https)

或者在您的情况下:

process.env['FIREBASE_AUTH_EMULATOR_HOST'] = 'localhost :9099'

然后你可以按照正常的

admin.initializeApp()

初始化应用程序,为我工作(在我最终发现不包括协议方案)

来源:https://firebase.google。 com/docs/emulator-suite/connect_auth#admin_sdks

Set the environment variable FIREBASE_AUTH_EMULATOR_HOST

export FIREBASE_AUTH_EMULATOR_HOST=localhost:9099

Do not include the protocol scheme (i.e http/https)

Or in your case:

process.env['FIREBASE_AUTH_EMULATOR_HOST'] = 'localhost:9099'

Then you can initialise the app as per normal

admin.initializeApp()

Worked for me (after I finally figured out not to include the protocol scheme)

source: https://firebase.google.com/docs/emulator-suite/connect_auth#admin_sdks

空城旧梦 2025-01-17 01:33:24

设置身份验证模拟器主机环境并在initializeApp中传递projectId

const admin = require('firebase-admin');

process.env['GCLOUD_PROJECT'] = 'my-firebase-project-id';
process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080';
process.env['FIREBASE_AUTH_EMULATOR_HOST'] = 'localhost:9099'; // add this line

const app = admin.initializeApp({projectId: 'your-project-id'}); // change this line

const auth = app.auth();

console.log('I have an auth service object');

auth.listUsers().then(users => console.log(users));

set auth emulator host env and pass projectId in initializeApp

const admin = require('firebase-admin');

process.env['GCLOUD_PROJECT'] = 'my-firebase-project-id';
process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080';
process.env['FIREBASE_AUTH_EMULATOR_HOST'] = 'localhost:9099'; // add this line

const app = admin.initializeApp({projectId: 'your-project-id'}); // change this line

const auth = app.auth();

console.log('I have an auth service object');

auth.listUsers().then(users => console.log(users));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文