doudao9896 2018-09-07 20:50
浏览 51

如何在本地计算机上获得Google Cloud API(Firestore)的权限

I am new to fireStore and trying to retrieve document values but it isn't working as I would expect. I am getting a "permission denied" when trying to access through my IDE and golang.

Code:

func TestConnectToCollection(t *testing.T) {

    ctx := context.Background()
    client, err := firestore.NewClient(ctx, "<my-Project-ID>")
    if err != nil {
        // TODO: Handle error.
    }
    defer client.Close()
    doc := client.Doc("profile/test3")
    fmt.Println(doc)
    fmt.Println(doc.ID)

    iter := client.Collection("profile").Documents(ctx)
    for {
        doc, error := iter.Next()
        if error == iterator.Done {
            break
        }
        if error != nil {
            fmt.Println(error.Error())
        } else {
            fmt.Println(doc.Data())
        }
    }
}

Output:

&{0xc0001725a0 projects/<project-id>/databases/(default)/documents/profile/test3 test3}
test3
rpc error: code = PermissionDenied desc = Missing or insufficient permissions.
rpc error: code = PermissionDenied desc = Missing or insufficient permissions.
rpc error: code = PermissionDenied desc = Missing or insufficient permissions.
rpc error: code = PermissionDenied desc = Missing or insufficient permissions.

Firestore rules - I opened up my firestore rules thinking that was the issue.

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}
  • 写回答

2条回答 默认 最新

  • dongzhila3786 2018-09-07 21:31
    关注

    The docs tell me that Doc() returns a *DocumentRef, a reference to the document. To fetch the document itself, you should call Get() on it, which gives you a *DocumentSnapshot, and finally call Data() on that.

    ref := client.Doc("profile/test3")
    snap, err := ref.Get(ctx)
    // FIXME error checking
    data := snap.Data()
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大