Last active
August 9, 2016 18:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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