简体   繁体   中英

How to connect to client in GCE in GO

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM