简体   繁体   中英

Golang and gcloud API: how to get an auth token

Because Google AutoML does not have a golang client, I have to use the AutoML http client. To do so, requires an auth token from google which comes from running the following cli command:

gcloud auth application-default print-access-token

I am currently authing my Golang server with a credentials json file that has access to AutoML as well (example usage)

storageClient, err := storage.NewClient(ctx, option.WithCredentialsFile(gcloudCredsJSONPath))

My question is: how would I get an auth token from the Golang Google client if I have a JSON credentials file? Is this even possible?

Thank you for any help!

You can only use API tokens with certain Google Cloud APIs. Using tokens is discourage by Google Cloud as you can read in this article:

https://cloud.google.com/docs/authentication/

If your production environment is also Google Cloud, you might not need to use any JSON file at all. Google Cloud has the concept of "DefaultCredentials" that it injects in your services via the environment. You might be able to simplify your code to:

storageClient, err := storage.NewClient(ctx)

It's also recommended to use a "ServiceAccount" so the credentials that your application use can be scopes to it. You can read more here:

https://cloud.google.com/docs/authentication/getting-started

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