Firebase Admin SDK for Golang
On Wednesday, May 17, 2017 Google announced at Google IO : Open sourcing the Firebase SDKs. But for now, there is no official Admin SDK for Golang, only Java, Node and Python Admin SDKs.
So welcome go-firebase-admin SDK :)
Note
If you decide to use this unofficial SDK still in development,
please use any package manager to fix version, there will be a lot of breaking changes.
Install the package with go:
go get github.com/acoshift/go-firebase-admin
This go-firebase-admin SDK supports the following functions :
-
Authentication
- CreateCustomToken : Generate JSON Web Tokens (JWTs) on your server, pass them back to a client device, and then use them to authenticate via the signInWithCustomToken() method.
- VerifyIDToken : verify the integrity and authenticity of the ID token and retrieve the uid from it.
-
User Management API
- GetUser : fetching the profile information of users by their uid
- GetUsers : fetching list of profile information of users by their uid
- GetUserByEmail : fetching the profile information of users by their email
- GetUsersByEmail : fetching list of profile information of users by their email
- GetUserByPhoneNumber : fetching the profile information of users by their phoneNumber
- GetUsersByPhoneNumber : fetching list of profile information of users by their phoneNumber
- ListUsers : fetching the profile information of users
- CreateUser : create a new Firebase Authentication user
- UpdateUser : modifying an existing Firebase user's data.
- DeleteUser : deleting existing Firebase user by uid
- SendPasswordResetEmail : send password reset for the given user
- VerifyPassword : verifies given email and password
-
Realtime Database API
- not documented
-
Cloud Messaging API
- SendToDevice : Send Message to individual device
- SendToDevices : Send multicast Message to a list of devices
- SendToDeviceGroup : Send Message to a device group
- SendToTopic : Send Message to a topic
- SendToCondition : Send a message to devices subscribed to the combination of topics
- SubscribeDeviceToTopic : subscribe a device to a topic
- SubscribeDevicesToTopic : subscribe devices to a topic
- UnSubscribeDeviceFromTopic : Unsubscribe a device to a topic
- UnSubscribeDevicesFromTopic : Unsubscribe devices to a topic
- update documentation
- add examples
- add tests
You can find more details about go-firebase-admin on godoc.org.
- Firebase Setup Guide
- Firebase Database Guide
- Firebase Authentication Guide
- Firebase Cloud Messaging Guide
- Firebase Cloud Messaging Server
- Firebase Release Notes
You need a service_account.json file, if you don't have an admin SDK service_account.json, please check this guide
You need a Firebase API Key for FCM, whose value is available in the Cloud Messaging tab of the Firebase console Settings panel
Initialize Firebase Admin SDK
package main
import (
"io/ioutil"
"google.golang.org/api/option"
"github.com/acoshift/go-firebase-admin"
)
func main() {
// Init App with service_account
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
ProjectID: "YOUR_PROJECT_ID",
}, option.WithCredentialsFile("service_account.json"))
if err != nil {
panic(err)
}
}
package main
import (
"io/ioutil"
"google.golang.org/api/option"
"github.com/acoshift/go-firebase-admin"
)
func main() {
// Init App with service_account
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
ProjectID: "YOUR_PROJECT_ID",
}, option.WithCredentialsFile("service_account.json"))
if err != nil {
panic(err)
}
// Firebase AUth
firAuth := firApp.Auth()
// VerifyIDToken
claims, err := firAuth.VerifyIDToken("My token")
// CreateCustomToken
myClaims := make(map[string]string)
myClaims["name"] = "go-firebase-admin"
myClaims["ID"] = "go-go-go"
cutomToken, err := firAuth.CreateCustomToken(claims.UserID, myClaims)
}
package main
import (
"io/ioutil"
"google.golang.org/api/option"
"github.com/acoshift/go-firebase-admin"
)
func main() {
// Init App with service_account
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
ProjectID: "YOUR_PROJECT_ID",
DatabaseURL: "YOUR_DATABASE_URL",
}, option.WithCredentialsFile("service_account.json"))
if err != nil {
panic(err)
}
// Firebase Database
firDatabase := firApp.Database()
type dinosaurs struct {
Appeared int64 `json:"appeared"`
Height float32 `json:"height"`
Length float32 `json:"length"`
Order string `json:"order"`
Vanished int64 `json:"vanished"`
Weight int `json:"weight"`
}
r := firDatabase.Ref("test/path")
err = r.Child("bruhathkayosaurus").Set(&dinosaurs{-70000000, 25, 44, "saurischia", -70000000, 135000})
if err != nil {
panic(err)
}
// Remove
err = r.Remove()
if err != nil {
panic(err)
}
// Snapshot
snapshot, err := r.OrderByChild("height").EqualTo(0.6).OnceValue()
if err != nil {
panic(err)
}
}
package main
import (
"io/ioutil"
"google.golang.org/api/option"
"github.com/acoshift/go-firebase-admin"
)
func main() {
// Init App with service_account
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
ProjectID: "YOUR_PROJECT_ID",
DatabaseURL: "YOUR_DATABASE_URL",
APIKey: "YOUR_API_KEY",
}, option.WithCredentialsFile("service_account.json"))
if err != nil {
panic(err)
}
// FCM
firFCM := firApp.FCM()
// SendToDevice
resp, err := firFCM.SendToDevice(context.Background(), "mydevicetoken",
firebase.Message{Notification: firebase.Notification{
Title: "Hello go firebase admin",
Body: "My little Big Notification",
Color: "#ffcc33"},
})
if err != nil {
panic(err)
}
// SendToDevices
resp, err := firFCM.SendToDevices(context.Background(), []string{"mydevicetoken"},
firebase.Message{Notification: firebase.Notification{
Title: "Hello go firebase admin",
Body: "My little Big Notification",
Color: "#ffcc33"},
})
if err != nil {
panic(err)
}
// SubscribeDeviceToTopic
resp, err := firFCM.SubscribeDeviceToTopic(context.Background(), "mydevicetoken", "/topics/gofirebaseadmin")
// it's possible to ommit the "/topics/" prefix
resp, err := firFCM.SubscribeDeviceToTopic(context.Background(), "mydevicetoken", "gofirebaseadmin")
if err != nil {
panic(err)
}
// UnSubscribeDeviceFromTopic
resp, err := firFCM.UnSubscribeDeviceFromTopic(context.Background(), "mydevicetoken", "/topics/gofirebaseadmin")
// it's possible to ommit the "/topics/" prefix
resp, err := firFCM.UnSubscribeDeviceFromTopic(context.Background(), "mydevicetoken", "gofirebaseadmin")
if err2 != nil {
panic(err)
}
}
MIT License
Copyright (c) 2016 Thanatat Tamtan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.