如何在 GO 中连接到 GCE 中的客户端

How to connect to client in GCE in GO

我无法连接到客户端。有没有办法将 private key idprivate key 传递给客户以获得授权?

所以,到目前为止我看到:

ctx := context.Background()

client, err := google.DefaultClient(ctx, compute.ComputeScope)
    if err != nil {
            //...
    }
computeService, err := compute.New(client)
    if err != nil {
            //...
    }

首先,需要在 GCE 中为服务帐户创建一个凭据,它将下载一个 json 文件,它将用于授权客户端。

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