英文:
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.ScopeDatastore
和 datastore.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),
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论