英文:
How to connect to client in GCE in GO
问题
我无法连接到客户端。有没有办法将“私钥ID”和“私钥”传递给客户端以进行授权?
到目前为止,我看到的是:
ctx := context.Background()
client, err := google.DefaultClient(ctx, compute.ComputeScope)
if err != nil {
//...
}
computeService, err := compute.New(client)
if err != nil {
//...
}
英文:
I can't able to connect to client. Is there any way to pass private key id
and private key
to client for get authorized?
So, far i see:
ctx := context.Background()
client, err := google.DefaultClient(ctx, compute.ComputeScope)
if err != nil {
//...
}
computeService, err := compute.New(client)
if err != nil {
//...
}
答案1
得分: 3
首先,需要在GCE中为服务账号创建凭据,它将下载一个JSON文件,并用于授权客户端。
示例代码:
data, err := ioutil.ReadFile("downloadedfile.json")
if err != nil {
log.Fatal(err)
}
conf, err := google.JWTConfigFromJSON(data, compute.ComputeScope) // 为该客户端指定特定权限。
if err != nil {
log.Fatal(err)
}
client = conf.Client(oauth2.NoContext)
computeService, err := compute.New(client)
if err != nil {
//...
}
英文:
Firstly, Need to create a credential for service account in GCE and it will download a json file and it will use for authorize the client.
Sample Code :
data, err := ioutil.ReadFile("downloadedfile.json")
if err != nil {
log.Fatal(err)
}
conf, err := google.JWTConfigFromJSON(data, compute.ComputeScope) // give the specific permission for this client.
if err != nil {
log.Fatal(err)
}
client = conf.Client(oauth2.NoContext)
computeService, err := compute.New(client)
if err != nil {
//...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论