1 回答

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊
不幸的是,沒有 Go 特定的文檔,但我相信,基于新文檔,舊的 REST 身份驗(yàn)證方式不再適用。話雖如此,我已經(jīng)能夠讓您的代碼工作,閱讀一堆文檔,大量試驗(yàn)和錯(cuò)誤,并通過 JWT 使用 OAuth 身份驗(yàn)證。
首先,請遵循本指南:https : //firebase.google.com/docs/server/setup,但只是“將 Firebase 添加到您的應(yīng)用程序”部分。
發(fā)出一個(gè)go get -u golang.org/x/oauth2和go get -u golang.org/x/oauth2/google(或使用您最喜歡的銷售方式)。
更改您的代碼,如下所示:
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)
}
}
以上對我有用!
- 1 回答
- 0 關(guān)注
- 156 瀏覽
添加回答
舉報(bào)