在 App Engine 标准环境下使用 Golang 初始化 Firebase Admin
时间:2024-03-25
浏览:179
作者:佚名
在 App Engine 标准环境中使用 Golang 初始化 Firebase Admin 时,开发者可能会遇到 403 错误,提示请求缺少有效的 API 密钥。这通常是因为开发者在传递 Firebase 凭据时使用错误的方法。 文章提供了两种解决方案: * **传递 nil 配置:** Firebase Admin 库可以根据 Google Cloud 上的凭据计算出凭据。因此,可以将配置传递为 nil,如下所示:`fbApp, err := fb.NewApp(ctx, nil, ops...)`。 * **传递凭据:**开发者也可以直接传递从 Firebase 控制台中获取的 JSON 凭据,如下所示:`creds, err := google.CredentialsFromJSON(ctx, credJSON, scopes...)`。
问题内容
我使用 golang 应用程序引擎标准。当我使用 firebase 时,我不断收到 403 错误。这是我用来传递 firebase 凭据的代码。这个 api 密钥是什么?我做错了什么?
错误: googleapi:错误 403:请求缺少有效的 api 密钥。禁止
credJSON := []byte("{...json from firebase console...}")
creds, err := google.CredentialsFromJSON(ctx, credJSON, "https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/datastore",
"https://www.googleapis.com/auth/devstorage.full_control",
"https://www.googleapis.com/auth/firebase",
"https://www.googleapis.com/auth/identitytoolkit",
"https://www.googleapis.com/auth/userinfo.email")
if err != nil {
return err
}
ops = append(ops, option.WithCredentials(creds))
fbApp, err := fb.NewApp(ctx, &fb.Config{ProjectID: projectID}, ops...)解决方案
事实证明,如果您传递 nil 进行配置。图书馆计算出 google cloud 上的凭据。所以,这是代码:
fbApp, err := fb.NewApp(ctx, nil, ops...)
if err != nil {
return nil, err
}文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《在 App Engine 标准环境下使用 Golang 初始化 Firebase Admin》文章吧,也可关注golang学习网公众号了解相关技术文章。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
