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:

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

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来