firebase - 如何在Go Server应用程序中配置凭据文件以使用Firebase模拟器

标签 firebase go google-app-engine google-cloud-firestore

我在Google App Engine上有一个服务器Go应用程序,该应用程序使用Firebase Auth和Firestore。

func InitFirebase() {
    ctx := context.Background()
    opt := option.WithCredentialsFile("keys/firebase.json")
    app, err := firebase.NewApp(ctx, nil, opt)
    if err != nil {
        panic(err)
    }
    FirebaseAuth, err = app.Auth(ctx)
    if err != nil {
        panic(err)
    }
    Firestore, err = app.Firestore(ctx)
    if err != nil {
        panic(err)
    }
}
它具有用于访问所有Firebase服务的json配置文件。 firebase.json是从Firebase控制台下载的,其中包含连接服务所需的所有参数:
{
  "type": "service_account",
  "project_id": "xxxx",
  "private_key_id": "xxxxx",
  "private_key": "-----BEGIN PRIVATE KEY----- xxxxx \n-----END PRIVATE KEY-----\n",
  "client_email": "xxxx.gserviceaccount.com",
  "client_id": "xxxx",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminxxxx.iam.gserviceaccount.com"
}
我必须输入什么值才能连接到Firebase Firestore Emulator并维护身份验证?

最佳答案

不需要更改json文件。
仅定义环境变量FIRESTORE_EMULATOR_HOST =“localhost:8080” 即可自动完成与仿真器的连接。
如果使用的是Visual Studio Code,请在launch.json文件中定义它:

 "configurations": [
    {
        "name": "Launch",
        "type": "go",
        "request": "launch",
        "mode": "auto",
        "program": "${fileDirname}",
        "env": {
            "FIRESTORE_EMULATOR_HOST": "localhost:8080"
        },
        "args": []
    }
]

关于firebase - 如何在Go Server应用程序中配置凭据文件以使用Firebase模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63377026/

相关文章:

ios - 检索要标记的 Firebase 数据

firebase - 设备上的 ML Kit Face Detection 是否可供商业公司免费使用?

firebase - 如何取消正在进行的云功能?

java - GAE 上传文件 POST 请求(URLConnection) - 错误 : the uploaded file was only partially uploaded

android - 为什么 AdMob 广告频次上限不起作用?

json - 无法将类型* json.RawMessage的表达式转换为golang中的[] byte类型

http - 如何为解析错误自定义 HTTP 400 响应?

go - 用于 Go 的 LiteIDE : Playground Errors

google-app-engine - 部署后的 Google Cloud Endpoint 404

python - 谷歌应用引擎: Difficulty with Users API (or maybe just a Python syntax problem)