英文:
Firebase Go wants host "firebaseio.com"
问题
我是你的中文翻译助手,以下是你要翻译的内容:
我对Firebase还不熟悉,我正在尝试在Go中设置一个小型测试,使用一个简单的数据库。我在数据库连接方面遇到了很多问题。以下是我的代码:
tx := context.Background()
conf := &firebase.Config{
DatabaseURL: "https://mydb.europe-west1.firebasedatabase.app",
}
// 获取服务账号密钥的JSON文件内容
opt := option.WithCredentialsFile("./fireBasePrivateKey.json")
// 使用服务账号初始化应用程序,授予管理员权限
app, err := firebase.NewApp(ctx, conf, opt)
if err != nil {
log.Fatalln("初始化应用程序时出错:", err)
}
client, err := app.Database(ctx)
if err != nil {
log.Fatalln("初始化数据库客户端时出错:", err)
}
使用这段代码(来自官方文档),我在初始化数据库客户端时遇到了一个错误:
无效的数据库URL:需要主机:.firebaseio.com
然后我尝试了请求的URL:mydb.firebaseio.com -> 我得到了另一个错误,告诉我我的数据库不在那个区域,并给出了之前的数据库地址。
我还尝试了其他一些东西,比如mydb.europe-west1.firebaseio.com,但这里告诉我证书对于这个URL无效...
我有点迷失了。我理解问题与我创建数据库时选择的数据库位置有关,但我不明白如何在Go实现中处理它。
英文:
I'm new to firebase and I'm trying to setup a small test with a simple database in Go.
I struggle a lot with the database connection. Here is my code:
tx := context.Background()
conf := &firebase.Config{
DatabaseURL: "https://mydb.europe-west1.firebasedatabase.app",
}
// Fetch the service account key JSON file contents
opt := option.WithCredentialsFile("./fireBasePrivateKey.json")
// Initialize the app with a service account, granting admin privileges
app, err := firebase.NewApp(ctx, conf, opt)
if err != nil {
log.Fatalln("Error initializing app:", err)
}
client, err := app.Database(ctx)
if err != nil {
log.Fatalln("Error initializing database client:", err)
}
With that code (which comes from the official documentation), I've got an error on the Database client initialization:
> invalid database url: wants host: .firebaseio.com
I then tried with the requested url: mydb.firebaseio.com -> I've got another error telling me my db is not in that region and gives me the previous db address.
I also tried other things like mydb.europe-west1.firebaseio.com but here it says me the certificate is not valid for this url...
I'm a bit lost. I understand the problem has to do with the localization of the DB I choose when I created it, but I don't understand how to handle it with the go implementation.
答案1
得分: 5
<projectname>.firebaseio.com
格式曾经是 Firebase 数据库 URL 的唯一格式,直到去年年初。如今,美国的数据库仍然使用该格式,但其他地区的数据库使用您所拥有的 <dbname><region>.firebasedatabase.app
格式。
对于较新的 URL 格式的支持是在 PR #423 中添加的,并在 Go Admin SDK 的 4.6 版本 中发布,该版本于六月发布。升级到此版本(或更高版本),以确保您不再收到错误消息。
英文:
The <projectname>.firebaseio.com
format used to be the only format for Firebase Database URLs until early last year. Nowadays, databases in the US still use that format, but databases in other regions use the <dbname><region>.firebasedatabase.app
format that you have.
Support for the newer URL format was added in PR #423 and released in version 4.6 of the Go Admin SDK, which was released in June. Upgrade to this version (or later), to ensure you don't get the error message anymore.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论