dousi0144
dousi0144
2018-07-20 14:02

构建包含Firebase凭证文件.json的golang

已采纳

I've written an Golang program designed to is parse a scv file and upload data to FireStore, this program was build to share with people who just write scv path to upload info.

I'm using this to use firebase-admin:

opt := option.WithCredentialsFile("path/to/serviceAccountKey.json")

This approach works fine when I do:

$ go run main.go

but when I build the project and execute the binary I got this:

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

I did read this approach but it means share firebase-config.json too, the idea of this Go program is just share the executable without any doc else.

Is there a way to build the program including the json or authenticate firebase-admin using json directly like this?:

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

I have no found documentation or examples using this method WithCredential.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douqulv6059 douqulv6059 3年前

    I don't know if this is the best way to solve this but this is the way I did it:

    1. I decided to place this script executable into /opt/< dir-name >/bin
    2. I wrote an extra function which creates the auth .json /opt/< dir-name >/auth.json
    3. All the script executes using this /opt/< dir-name >/auth.json file
    4. I write another func which deletes the auth.json when the execution ends

    So, now I can do this:

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

    Now I can share this script with the directive "Install into /opt/< dir-name>/bin and add this to your $PATH"

    点赞 评论 复制链接分享
  • dqf98772 dqf98772 3年前

    For me, I use $GOPATH to fix this issue

    1. For example my $GOPATH is /home/Frank/work
    2. I put my .json file at /home/Frank/work/myfile.json
    3. I change my code to

      opt := option.WithCredentialsFile("/home/Frank/work/myfile.json")

    And somehow it works. :D

    点赞 评论 复制链接分享

相关推荐