dougan7657 2015-12-14 09:07
浏览 120
已采纳

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

I'm trying to insert a simple record as in GoDoc. But this 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've enabled Big table in my console and created a cluster and a service account and recieved the 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())
}
}
  • 写回答

2条回答 默认 最新

  • dsd119120 2015-12-16 04:12
    关注

    I've solved the problem. It's nothing wrong with the code, but config json itself. 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. 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. 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.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)