Google Cloud Bigtable with Go Support

I am trying to insert a simple entry, as in GoDoc. But it returns

rpc error: code = 7 desc = "User can't access project: tidy-groove"

When I searched for grpc codes, it says.

PermissionDenied Code = 7

// Unauthenticated indicates the request does not have valid
// authentication credentials for the operation.

I turned on the Big table in the console and created a cluster and a service account and got json. What am I doing wrong here?

package main

import (
"fmt"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/cloud"
"google.golang.org/cloud/bigtable"
"io/ioutil"
)

func main() {
fmt.Println("Start!")
put()
}

func getClient() *bigtable.Client {
jsonKey, err := ioutil.ReadFile("TestProject-7854ea9op741.json")
if err != nil {
    fmt.Println(err.Error())
}

config, err := google.JWTConfigFromJSON(
    jsonKey,
    bigtable.Scope,
) // or bigtable.AdminScope, etc.

if err != nil {
    fmt.Println(err.Error())
}

ctx := context.Background()
client, err := bigtable.NewClient(ctx, "tidy-groove", "asia-east1-b", "test1-bigtable", cloud.WithTokenSource(config.TokenSource(ctx)))

if err != nil {
    fmt.Println(err.Error())
}

return client
}

func put() {
ctx := context.Background()
client := getClient()
tbl := client.Open("table1")
mut := bigtable.NewMutation()
mut.Set("links", "maps.google.com", bigtable.Now(), []byte("1"))
mut.Set("links", "golang.org", bigtable.Now(), []byte("1"))
err := tbl.Apply(ctx, "com.google.cloud", mut)
if err != nil {
    fmt.Println(err.Error())
}
}
+4
source share
2 answers

I solved the problem. There is nothing wrong with the code, but config json itself. So anyone out there who wants to authenticate and come here using google search ... This code is correct and works fine. I did wrong.

json. google , , , , json. . , . / json .

... . . - . , .

! , , ( , ), . , :)

+3

: helloworld.go search.go, GOOGLE_APPLICATION_CREDENTIALS.

GOOGLE_APPLICATION_CREDENTIALS. Google, Google App Engine, . , gcloud init gcloud auth login, gcloud config set project <projectID>.

+1

Source: https://habr.com/ru/post/1619826/


All Articles