Skip to content

Instantly share code, notes, and snippets.

@abeisgoat
Last active August 9, 2016 18:54
Show Gist options
  • Save abeisgoat/0ebc6b7da222c0a1d9d1b1774d9a4c55 to your computer and use it in GitHub Desktop.
Save abeisgoat/0ebc6b7da222c0a1d9d1b1774d9a4c55 to your computer and use it in GitHub Desktop.
Connect to the Firebase Realtime Database in Golang using the Google API *http.Client
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"golang.org/x/oauth2/google"
)
func main() {
path := "https://[Your Namespace].firebaseio.com/restricted.json"
err := os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "creds.json")
hc, err := google.DefaultClient(context.Background(),
"https://www.googleapis.com/auth/firebase.database",
"https://www.googleapis.com/auth/userinfo.email",
)
if err != nil {
log.Fatalln(err)
}
req, err := http.NewRequest("GET", path, nil)
if err != nil {
log.Fatalln(err)
}
res, err := hc.Do(req)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s", body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment