Connecting to google cloud datastore using go

huangapple go评论91阅读模式
英文:

Connecting to google cloud datastore using go

问题

我正在尝试从Go语言连接到云数据存储。我使用了这里给出的示例代码 - https://github.com/GoogleCloudPlatform/gcloud-golang

以下是我代码中相关的部分:

func getCtx() context.Context {
    // 使用Google Developers Console JSON密钥初始化授权传输。
    // 阅读google包的示例以了解更多关于不同授权流程的信息。
    // http://godoc.org/golang.org/x/oauth2/google
    opts, err := oauth2.New(
        google.ServiceAccountJSONKey("CassandraTest-key.json"),
        oauth2.Scope(datastore.ScopeDatastore),
    )
    if err != nil {
        log.Fatal(err)
    }

    // titanium-goods-766是CassandraTest项目的项目ID(sthilakan@eyeota.com下)
    ctx := cloud.NewContext("titanium-goods-766", &http.Client{Transport: opts.NewTransport()})

    // 使用上下文(参见其他示例)
    return ctx
}

type contactInfoEntity struct {
    EmailKey  *datastore.Key
    FirstName string
    LastName  string
}

func main() {
    ctx := getCtx()
    fmt.Println("成功获取上下文", ctx)

    err := putEntity(ctx, "fname1", "lname1", "email1")

    if err != nil {
        fmt.Println("错误:", err)
    } else {
        fmt.Println("成功")
    }
}

func putEntity(ctx context.Context, firstName string, lastName string, email string) error {
    key := datastore.NewKey(ctx, "contactInfoEntity", email, 0, nil)

    contactInfoEntity := contactInfoEntity{
        EmailKey:  key,
        FirstName: firstName,
        LastName:  lastName,
    }

    _, err := datastore.Put(ctx, key, &contactInfoEntity)

    return err
}

我一直得到这个错误。

错误: error during call, http status code: 403 Unauthorized.

我已经多次禁用和重新启用数据存储API(如此处建议:https://stackoverflow.com/questions/17885347/all-requests-return-403-unauthorized)。我还尝试过删除和添加服务帐号。

(我尝试使用此处的步骤将我的计算引擎实例连接到数据存储,它可以正常工作)。

有人能够从Go语言连接到云数据存储吗?

谢谢,
Sathya

英文:

I am trying to connect to cloud datastore from Go. I used the sample code given here - https://github.com/GoogleCloudPlatform/gcloud-golang.

These are the relevant bits of my code:

func getCtx() context.Context {
// Initialize an authorized transport with Google Developers Console
// JSON key. Read the google package examples to learn more about
// different authorization flows you can use.
// http://godoc.org/golang.org/x/oauth2/google
opts, err := oauth2.New(
google.ServiceAccountJSONKey("CassandraTest-key.json"),
oauth2.Scope(datastore.ScopeDatastore),
)
if err != nil {
log.Fatal(err)
}
//titanium-goods-766 is the project id for CassandraTest (under sthilakan@eyeota.com)
ctx := cloud.NewContext("titanium-goods-766", &http.Client{Transport: opts.NewTransport()})
// Use the context (see other examples)
return ctx
}
type contactInfoEntity struct {
EmailKey  *datastore.Key
FirstName string
LastName  string
}
func main() {
ctx := getCtx()
fmt.Println("successfully got context", ctx)
err := putEntity(ctx, "fname1", "lname1", "email1")
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("success")
}
}
func putEntity(ctx context.Context, firstName string, lastName string, email string) error {
key := datastore.NewKey(ctx, "contactInfoEntity", email, 0, nil)
contactInfoEntity := contactInfoEntity{
EmailKey:  key,
FirstName: firstName,
LastName:  lastName,
}
_, err := datastore.Put(ctx, key, &contactInfoEntity)
return err
}

I get this error consistently.

Error: error during call, http status code: 403 Unauthorized.

I have disabled and reenabled datastore api a few times (as suggested here: https://stackoverflow.com/questions/17885347/all-requests-return-403-unauthorized). I have also tried removing and adding the service account.

(I tried to connect my compute engine instance to datastore using the steps here - https://cloud.google.com/datastore/docs and it works fine).

Have anyone connected to cloud datastore from go ?

Regards,
Sathya

答案1

得分: 3

访问 Cloud Datastore 需要两个范围:datastore.ScopeDatastoredatastore.ScopeUserEmail

opts, err := oauth2.New(
    google.ServiceAccountJSONKey("CassandraTest-key.json"),
    oauth2.Scope(datastore.ScopeDatastore, datastore.ScopeUserEmail),
)
英文:

Accessing Cloud Datastore requires two scopes: datastore.ScopeDatastore and datastore.ScopeUserEmail:

opts, err := oauth2.New(
google.ServiceAccountJSONKey("CassandraTest-key.json"),
oauth2.Scope(datastore.ScopeDatastore, datastore.ScopeUserEmail),
)

huangapple
  • 本文由 发表于 2014年12月15日 16:17:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/27479833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定