繁体   English   中英

使用Go进行Google Cloud Bigtable身份验证

[英]Google Cloud Bigtable authentication with Go

我正在尝试在GoDoc中插入一个简单的记录。 但这会回来,

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

当我搜索grpc代码时,它说..

PermissionDenied Code = 7

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

我在我的控制台中启用了Big table并创建了一个集群和一个服务帐户并收到了json。 我在这里做错了什么?

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

我已经解决了这个问题。 代码没什么问题,但配置json本身。 因此,那里的任何人都想通过谷歌搜索进行身份验证并来到这里...这段代码是正确的,并且工作正常。 我做错了以下是。

首先,我创建了一个服务帐户并获得了json。 但谷歌警告我,我不是项目的所有者因此它不会被添加到接受列表,但无论如何它让我下载json。 然后我从控制台删除了该密钥,并请求项目所有者为我创建密钥。 在那里,他创建了另一个与我给出的名称相同的密钥。由于他是所有者,没有显示错误/警告信息并且已成功下载json文件。

当我尝试这个时......我的问题就开始了。 那是我发布这个问题的时候。 之后没有解决方案。 我要求所有者删除该密钥并创建另一个密钥,但名称不同。

然后它工作了! 看起来如果您尝试使用非所有者帐户创建密钥,然后再次使用相同名称创建(在删除原始文件后)无效。 希望这有助于每个人:)

请查看: helloworld.gosearch.go ,它使用GOOGLE_APPLICATION_CREDENTIALS环境变量。

对于大多数环境,您甚至不再需要设置GOOGLE_APPLICATION_CREDENTIALS Google云端平台托管虚拟机Google App Engine都可以为您设置正确的功能。 如果你使用了gcloud init或它的前身gcloud auth login然后是gcloud config set project <projectID>你的桌面环境也将是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM