Node.js – 使用服务帐户密钥或 Firebase 配置文件初始化 Firebase?

huangapple go评论73阅读模式
英文:

Node JS - Initialize Firebase using service account key or Firebase config file?

问题

我试着学习如何在Node.js中使用Firebase,但有一点我无法理解。我已经寻找了几天的解释,但就是找不到有用的信息。

在按照一些教程的步骤后,我感到困惑,因为其中一些使用了serviceAccountKey文件,看起来像这样:

    "type": "service_account",
    "project_id": 
    "private_key_id": 
    "private_key":
    "client_id": 
    "auth_uri": 
    "token_uri": 
    "auth_provider_x509_cert_url": 
    "client_x509_cert_url":

然后他们这样初始化它:

admin.initializeApp({
    credential: admin.credential.cert(credentials)
});

而其他人使用了在Firebase注册应用程序时生成的Firebase配置文件,它看起来像这样:

const firebaseConfig = {
  apiKey: 
  authDomain:
  projectId:
  storageBucket:
  messagingSenderId: 
  appId:
  measurementId:
}

这两种方式有什么区别?何时应该使用第一种方式或第二种方式?
如果有相关的文档,请帮我查找,因为我找不到相关信息。

英文:

I'm trying to learn how to use Firebase with Node JS and there's a thing I can't understand. I've been looking for an explanation for a few days now, but I just can't find something useful.

After following some tutorials, I'm confused because some of them use the serviceAccountKey file which looks like

    "type": "service_account",
    "project_id": 
    "private_key_id": 
    "private_key":
    "client_id": 
    "auth_uri": 
    "token_uri": 
    "auth_provider_x509_cert_url": 
    "client_x509_cert_url":

and then they initialize it like this

admin.initializeApp({
    credential: admin.credential.cert(credentials)
});

While others use the Firebase configuration file which is generated when you register the app on Firebase, which looks like this:

const firebaseConfig = {
  apiKey: 
  authDomain:
  projectId:
  storageBucket:
  messagingSenderId: 
  appId:
  measurementId:
}

What is the difference? When are you supposed to use the first way or the second way?
If there is some documentation about this, please help me with it because I can't find anything relevant.

答案1

得分: 0

第一个提到的是用于后端的/Admin SDK代码。

第二个是用于前端的/web SDK

这些SDK不兼容,并且需要不同的初始化方法。

英文:

The first one you mentioned is for backend / Admin SDK code.

The second one is for the frontend / web SDK.

These SDKs are not compatible with each other and require different ways to initialize them.

huangapple
  • 本文由 发表于 2023年4月4日 03:34:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定