登录
首页 >  Golang >  Go问答

配置 Go 服务器应用程序以在 Firebase 模拟器中使用凭据文件

来源:stackoverflow

时间:2024-02-26 09:54:23 209浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《配置 Go 服务器应用程序以在 Firebase 模拟器中使用凭据文件》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我在 google app engine 上有一个使用 firebase auth 和 firestore 的服务器 go 应用程序。

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)
    }
}

它有一个 json 配置文件来访问所有 firebase 服务。 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 模拟器并维护身份验证?


解决方案


不需要改变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": []
    }
]

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《配置 Go 服务器应用程序以在 Firebase 模拟器中使用凭据文件》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>