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.

Prerequisites

Steps

  1. Enable the Cloud Vision API for your project: https://console.cloud.google.com/apis/api/vision.googleapis.com/overview
  2. If you haven’t already, create a JSON key for the service account running the Compute Engine instance: https://console.cloud.google.com/iam-admin/serviceaccounts/project. 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.

Weekend Reading for Dec 04

Here’s a curated list of the articles I read this weekend.

Negative feedback antipatterns
Charles-Axel shares a template for providing negative feedback in a constructive manner.
via Software Lead Weekly

The Books I Recommend For The New Manager
I purchased Managing Oneself after reading this post. Google Play had it for less than $7.
via Software Lead Weekly

Visual Studio Development – Productivity Enhancements in Visual Studio 2017 RC
Check out some of the productivity enhancements coming in the new Visual Studio.
via MSDN Magazine

Introducing Rager

Rager is service that notifies you when the projects you care about most publish a new release. After signing up using your GitHub account, you can start “watching” your favorite projects hosted by GitHub, NuGet or npm. Every night Rager scans for new releases. If it finds one that you are “watching”, it sends you an email with a link to the new release!

Continue reading Introducing Rager