0

I am using fireauth and firego from zabawaba99. I am getting an error (please see below) when pushing data to my firebase database. I have been following his examples but I cannot get it to work. Someone got a idea why this is happening?

Error:

2016/06/03 14:30:13 {
  "error" : "Failed to validate MAC."
}

Code:

gen := fireauth.New("<API-KEY/SECRET>")

data := fireauth.Data{"uid": "1"}
token, err := gen.CreateToken(data, nil)
if err != nil {
    log.Fatal(err)
}

fb := firego.New("https://myapp.firebaseio.com" , nil)
log.Println(token)
fb.Auth(token)

for i := 0; i<len(items); i++  {

    item := items[i]

    pushedItem, err := fb.Child("items").Push(items)
    if err != nil {
        log.Fatal(err) // error is happening here
    }

    var itemTest string
    if err := pushedItem.Value(&itemTest); err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%s: %s\n", pusedItem, itemTest)

}
tomwassing
  • 925
  • 1
  • 10
  • 30

1 Answers1

2

Unfortunately there isn't Go-specific documentation, but I believe, based on the new docs, that the old REST way to authenticate doesn't work any longer. Having said that, I have been able to get your code to work reading a bunch of docs, lots of trial & error, and by using OAuth authentication by means of JWT.

Firstly, follow this guide: https://firebase.google.com/docs/server/setup but just the "Add Firebase to your App" section.

Issue a go get -u golang.org/x/oauth2 and go get -u golang.org/x/oauth2/google (or use your favorite vendoring way).

Change your code as such:

package main

import (
    "fmt"
    "io/ioutil"
    "log"

    "github.com/zabawaba99/firego"
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
)

func main() {
    jsonKey, err := ioutil.ReadFile("./key.json") // or path to whatever name you downloaded the JWT to
    if err != nil {
        log.Fatal(err)
    }
    conf, err := google.JWTConfigFromJSON(jsonKey, "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/firebase.database")
    if err != nil {
        log.Fatal(err)
    }

    client := conf.Client(oauth2.NoContext)

    fb := firego.New("https://myapp.firebaseio.com" , client)

    for i := 0; i<len(items); i++  {
        item := items[i]

        pushedItem, err := fb.Child("items").Push(items)
        if err != nil {
            log.Fatal(err) // error is happening here
        }

        var itemTest string
        if err := pushedItem.Value(&itemTest); err != nil {
            log.Fatal(err)
        }

        fmt.Printf("%s: %s\n", pusedItem, itemTest)

    }
}

The above worked for me!

Edit: Adding reference to StackOverflow answers that helped me:

Community
  • 1
  • 1
GVdP
  • 165
  • 8