如何在本地计算机上获得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;
    }
  }
}

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()

I had the same problem, It's sad documentation only provides the most difficult ways to do it, as beginner it's important to make as easy as posible at first, but anyway these are the easiest steps to do it:

If you already have log in the console, jump to step 5

  1. Intall gcloud
  2. Run on your command line gcloud auth login
  3. Login with the google account that owns the project
  4. In your code, your configuration client should be like this:

    ctx := context.Background()
    client, err := firestore.NewClient(ctx, projectID)
    if err != nil {
        log.Fatal(err)
    }
    
  5. Run on your command line gcloud beta auth application-default login

And that's it! it's like magic, I had too much headache trying the other ways. This works for cloud storage too and any google cloud api I'll guess

About your code:

client.Doc("profile/test3")

You are not getting any document at all there, it's just creat a *DocumentRef, in order to get the document you should do:

    ctx:=context.Background()
    snapShot,err:=client.Doc("profile/test3").Get(ctx)
    if err!=nil{
    log.Println(err)
    return err
    }
    var myStruct myStructType
    if err=snapShot.DataTo(&myStruct);err!=nil{
    log.Println(err)
    return err
    }

Read documentation for more info