简体   繁体   English

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

[英]Google Cloud Bigtable authentication with Go

I'm trying to insert a simple record as in GoDoc. 我正在尝试在GoDoc中插入一个简单的记录。 But this returns, 但这会回来,

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

When I searched for grpc codes, it says.. 当我搜索grpc代码时,它说..

PermissionDenied Code = 7

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

I've enabled Big table in my console and created a cluster and a service account and recieved the json. 我在我的控制台中启用了Big table并创建了一个集群和一个服务帐户并收到了json。 What I'm 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())
}
}

I've solved the problem. 我已经解决了这个问题。 It's nothing wrong with the code, but config json itself. 代码没什么问题,但配置json本身。 So anyone who out there want to authenticate and came here by google search... This code is correct and working perfectly. 因此,那里的任何人都想通过谷歌搜索进行身份验证并来到这里...这段代码是正确的,并且工作正常。 What I've done wrong is follows. 我做错了以下是。

First I made a service account and got the json. 首先,我创建了一个服务帐户并获得了json。 But google warned me that im not an owner of project hence it wont be added to accept list but anyway it let me download the json. 但谷歌警告我,我不是项目的所有者因此它不会被添加到接受列表,但无论如何它让我下载json。 Then I deleted that key from console and requested project owner to create a key for me. 然后我从控制台删除了该密钥,并请求项目所有者为我创建密钥。 There he has created another key with the same name I given.. And since he's the owner no error/warning msgs displayed and successfully json file was downloaded. 在那里,他创建了另一个与我给出的名称相同的密钥。由于他是所有者,没有显示错误/警告信息并且已成功下载json文件。

When I tried with that... my question begun. 当我尝试这个时......我的问题就开始了。 That's when i posted this question. 那是我发布这个问题的时候。 After that with no solutions. 之后没有解决方案。 I asked owner to delete that key and create another key but with a different name.. 我要求所有者删除该密钥并创建另一个密钥,但名称不同。

Then it worked! 然后它工作了! It seems if you try to create a key with non-owner account and then again create with same name ( after deleting original of course ) has no effect. 看起来如果您尝试使用非所有者帐户创建密钥,然后再次使用相同名称创建(在删除原始文件后)无效。 Hope this helps everyone out there :) 希望这有助于每个人:)

Take a look at: helloworld.go or search.go which uses GOOGLE_APPLICATION_CREDENTIALS environment variable. 请查看: helloworld.gosearch.go ,它使用GOOGLE_APPLICATION_CREDENTIALS环境变量。

For most environments, you no longer even need to set GOOGLE_APPLICATION_CREDENTIALS . 对于大多数环境,您甚至不再需要设置GOOGLE_APPLICATION_CREDENTIALS Google Cloud Platform , Managed VMs or Google App Engine all have the right thing set for you. Google云端平台托管虚拟机Google App Engine都可以为您设置正确的功能。 Your desktop environment will also be correct if you've used gcloud init or it's predecessor gcloud auth login followed by gcloud config set project <projectID> . 如果你使用了gcloud init或它的前身gcloud auth login然后是gcloud config set project <projectID>你的桌面环境也将是正确的。

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

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