doudi1979 2016-06-03 12:46
浏览 49
已采纳

Firebase:无法验证MAC

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
", pusedItem, itemTest)

}
  • 写回答

1条回答 默认 最新

  • donglu8334 2016-06-07 17:16
    关注

    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
    ", pusedItem, itemTest)
    
        }
    }
    

    The above worked for me!

    Edit: Adding reference to StackOverflow answers that helped me:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码