登录
首页 >  Golang >  Go问答

构建 golang,包括 firebase 凭证文件 .json

来源:stackoverflow

时间:2024-04-12 17:39:33 259浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《构建 golang,包括 firebase 凭证文件 .json》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我编写了一个 golang 程序,旨在解析 scv 文件并将数据上传到 firestore,该程序的构建是为了与只编写 scv 路径来上传信息的人共享。

我用它来使用 firebase-admin:

opt := option.withcredentialsfile("path/to/serviceaccountkey.json")

当我这样做时,这种方法效果很好:

$ go run main.go

但是当我构建项目并执行二进制文件时,我得到了这个:

$ cannot read credentials file: open firebase-config.json: no such file or directory

我确实读过这种方法,但这也意味着共享 firebase-config.json, 这个 go 程序的想法只是共享可执行文件,没有任何其他文档。

有没有办法构建包含 json 的程序或像这样直接使用 json 验证 firebase-admin ?:

opt := option.WithCredentials({< authentication json >})

我没有找到使用此方法的文档或示例withcredential


解决方案


我不知道这是否是解决这个问题的最佳方法,但我就是这样做的:

  1. 我决定将此脚本可执行文件放入 /opt/< dir-name >/bin
  2. 我编写了一个额外的函数来创建 auth .json /opt/< dir-name >/auth.json
  3. 所有脚本均使用此 /opt/< dir-name >/auth.json 文件执行
  4. 我编写了另一个函数,它会在执行结束时删除auth.json

所以,现在我可以这样做:

var file string = "/opt/< dir-name >/auth.json" 
sa := option.WithCredentialsFile(file)

现在我可以使用指令“安装到 /opt/< dir-name>/bin 并将其添加到您的 $path”指令共享此脚本

对我来说,我使用 $gopath 来解决这个问题

  1. 例如,我的 $gopath 是 /home/frank/work
  2. 我将 .json 文件放在 /home/frank/work/myfile.json
  3. 我将代码更改为

    opt := option.withcredentialsfile("/home/frank/work/myfile.json")

不知怎的,它确实有效。 :d

以上就是《构建 golang,包括 firebase 凭证文件 .json》的详细内容,更多关于的资料请关注golang学习网公众号!

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