仅在Golang中使用环境变量初始化Firebase Admin SDK

我正在尝试在运行中仅使用环境变量(无法访问文件系统)初始化Firebase Admin SDK 。</ p>

我有< code> service-account-file.json </ code>文件,并可以通过以下方式使其在本地运行:</ p>

  export GOOGLE_APPLICATION_CREDENTIALS =“ / path / to / service-account-  file.json“ 

...

myfile.go:
app,err:= firebase.NewApp(context.Background(),nil)
</ code> </ pre>

但是,我想将 service-account-file.json </ code>的内容放入一个env变量中,并使用其值初始化firebase Admin SDK。</ p>
\ n

有一个未解决的问题要求您 可以通过 CredentialsFromJSON 完成,但是我不能 可以解决这个问题。</ p>

有人能用吗? 如果可以,请提供示例吗?</ p>
</ div>

展开原文

原文

I'm trying to initialize the Firebase Admin SDK, in go, using only environment variables (no access to a filesystem).

I have the service-account-file.json file, and can get it working locally via:

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-file.json"

...

myfile.go:
app, err := firebase.NewApp(context.Background(), nil)

However, I'd like to put the contents of service-account-file.json into an env var, and init the firebase Admin SDK with its value.

There is an open issue that claims you can do it via CredentialsFromJSON, however I can't quite figure it out.

Does anyone have this working? If so can you please provide an example?

1个回答

try:

export JSON_CREDS="{\"type\":\"service_account\",\"project_id\":\"project-id\",\"private_key_id\":\"some_number\",\"private_key\":\"\",\"client_email\":\"<api-name>api@project-id.iam.gserviceaccount.com\",\"client_id\":\"...\",\"auth_uri\":\"https:\/\/accounts.google.com\/o\/oauth2\/auth\",\"token_uri\":\"https:\/\/accounts.google.com\/o\/oauth2\/token\",\"auth_provider_x509_cert_url\":\"https:\/\/www.googleapis.com\/oauth2\/v1\/certs\",\"client_x509_cert_url\":\"https:\/\/www.googleapis.com\/...<api-name>api%40project-id.iam.gserviceaccount.com\"}"

...

import "google.golang.org/api/option"

...

app, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsJSON([]byte(os.Getenv("JSON_CREDS"))))
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐