构建包含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个回答

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"



对于我来说,我使用$ GOPATH来解决此问题</ p>


  1. 例如,我的$ GOPATH是 / home / Frank / work </ strong> </ li> \ li>
  2. 我将.json文件放在 /home/Frank/work/myfile.json </ strong > </ li>
  3. 我将代码更改为</ p>

    opt:=选项。WithCredentialsFile(“ / home / Frank / work / myfile.json”)< / p> </ li>
    </ ol>

    ,并且以某种方式起作用。 :D </ p>
    </ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问