How to connect to client in GCE in GO

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

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 {
        //...
}

huangapple
  • 本文由 发表于 2015年9月26日 09:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/32792856.html
匿名

发表评论

匿名网友

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

确定