dougan1465 2019-06-14 22:42
浏览 251
已采纳

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

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条回答 默认 最新

  • dplbf4340 2019-06-16 01:18
    关注

    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"))))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!