使用go连接到谷歌云数据存储 [英] Connecting to google cloud datastore using go
问题描述
我试图从Go连接到云数据存储。我使用了此处给出的示例代码 - https://github.com/GoogleCloudPlatform/gcloud-golang
这些是我的代码的相关位:
func getCtx()context.Context {
//使用Google Developers Console初始化授权的传输
// JSON密钥。阅读谷歌包示例,了解更多有关
// //您可以使用的不同授权流程。
// 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的项目编号(位于sthilakan@eyeota.com)
ctx:= cloud.NewContext(titanium-goods-766,& http.Client {Transport:opts.NewTransport()})
//使用上下文(请参阅其他示例)
return ctx
}
类型contactInfoEntity struct {
EmailKey * datastore.Key
名字字符串
姓氏字符串
}
func main(){
ctx:= getCtx()
fmt.Println(成功获取上下文,ctx)
err:= putEntity(ctx,fname1,lname1,email1)
if err!= nil {
fmt.Println(Error:,err)
} else {
fmt.Printl n(成功)
}
}
func putEntity(ctx context.Context,名字字符串,姓氏字符串,电子邮件字符串)错误{
key:= datastore.NewKey(ctx,contactInfoEntity,email,0,nil)
contactInfoEntity:= contactInfoEntity {
EmailKey:key,
名字:firstName,
LastName :姓氏,
}
_,err:= datastore.Put(ctx,key,&contactInfoEntity)
返回错误
}
我始终得到此错误。
错误:呼叫期间出错,http状态码:403未经授权。
我禁用并重新启用了数据存储api几次(如以下建议:所有请求返回403未授权)。我也尝试删除并添加服务帐户。
(我尝试使用这里的步骤将我的计算引擎实例连接到数据存储 - https://cloud.google.com/datastore/docs ,它工作正常)。
有人连接到云数据存储从去?
问候,
Sathya
访问云数据存储需要两个范围: datastore.ScopeDatastore
和 datastore.ScopeUserEmail
:
opts,err:= oauth2.New(
google.ServiceAccountJSONKey(CassandraTest-key.json),
oauth2.Scope(datastore.ScopeDatastore, datastore.ScopeUserEmail),
)
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: 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
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),
)
这篇关于使用go连接到谷歌云数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!