Authenticating Google API Client Library for Go using Service Accounts
This post is specifically for the autogenerated Google APIs Client for Go. I haven’t tried the other Google Cloud Library for Go since it didn’t have the compute library I needed.
You can use the
golang.org/x/oauth2/google library for authentication with this library. It can work with service accounts as well, which is what I am using at the moment.
Using the GOOGLE_APPLICATION_CREDENTIALS environment variable
After you have created your service account, downloaded the JSON file, and saved it in some location, you can set the
GOOGLE_APPLICATION_CREDENTIALS environment variable with the path of your service account JSON file.
With that in place, the Application Default Credentials Example sample code should just work for you.
Using the service account file directly to create the client
It’s all fine and good if you only use one service account file all throughout the application but if you happen to need to use a specific JSON file per call, this section is for you.---
If you have any questions or feedback, please reach out @flowerinthenyt.
This work is licensed under a Creative Commons Attribution 4.0 International License.