Authenticate Cloud Vision Client in Go

Google’s Cloud Platform documentation is top of the line. Even with the documentation though, it took me some time to figure out how to use the Cloud Vision API from a Go application running on a Compute Engine instance. All the right information is there, but it’s scattered. Below I’ve compiled this information into a few steps.

Authenticating with the Cloud Vision API involves more setup than the Cloud Datastore, Cloud Pub/Sub and Cloud Storage. The client libraries for the latter use  Google Application Default Credentials and “just work” when running on a Compute Engine instance. In order to authenticate with the Cloud Vision API, you need to create a service account key and instruct the client library to use that key when sending API requests. See Authenticating to the Cloud Vision API for more details.



  1. Enable the Cloud Vision API for your project:
  2. If you haven’t already, create a JSON key for the service account running the Compute Engine instance: Click the dropdown beside the service account, click Create Key and select the JSON key type.
  3. Copy the key JSON file to your Compute Engine instance
  4. When creating the Vision API client in Go, provide the service account file with option.WithServiceAccountFile("/path/to/key.json"):
    visionClient, err := vision.NewImageAnnotatorClient(ctx, option.WithServiceAccountFile("/path/to/key.json"))
        if err != nil {
            return nil, nil, errors.Wrap(err, "Error creating vision client")

Failed to get D-Bus connection

After setting up the systemd service for Caddy, I received the following error when trying to start the service:

$ sudo systemctl start caddy.service
Job for caddy.service failed. See 'systemctl status caddy.service' and 'journalctl -xn' for details.
$ systemctl status caddy.service
Failed to get D-Bus connection: No such file or directory

The fix was pretty easy. It turns out that dbus was not installed on the system. I installed it and then the service worked perfectly:

$ apt-get install -y dbus

For what it’s worth, I did this on a Google Cloud Compute Engine instance that I initialized with the default Debian install.