JWT, Google Cloud API, NLP, Delphi

Andrei Aleksandrov
Google Cloud - Community
4 min readMay 31, 2018

Hi!

At the hackathon last week we tried to build a solution for text processing. It didn’t happen, but I learned how to consume Google NLP API from Delphi and I think it’s worth sharing. These instructions can also be applied for other Google Cloud API’s, but not all, because different API’s have different authentication methods.

Okay, less talking, more coding! Let’s start!

Enable the API

First of all, you need to register for a Google Cloud. I’ll not describe this process, only one thing you need to know that this requires your credit card. After that you should land here:

The main page of Google Cloud Console

The next step is to click “Create project”. As you see, I have one called “default”. Let’s open it and you will see a dashboard of your project:

Dashboard

Now click on “Go to APIs overview”:

API dashboard

Click on “Library” and find “Natural language API”

Now click on this and then on “Enable” button. Attention: all further steps cost money (but if you have registered for trial, you have a 300 dollar credit)

E-e-e-nable!

Getting credentials

In API dashboard, click on “Credentials” and then “Create credentials” and select “service account key”:

Credentials view

Fill all the fields and select “JSON”

I’m not sure what should be in a “Role” box

After that you get a JSON file:

This file is very secret and save it somewhere, where nobody can find it!

Prepare

To create a JWT, you need to create a JSON Web Signature which can be created using an RSA key. To get a correct RSA key you need to copy a value of “private_key” of your secret JSON file and replace all “\n” with a line break. In the end you should get the following:

The key is disabled, but to be sure, I added a black rectangle

You also need two more things:

  1. Delphi JOSE and JWT library
  2. OpenSSL DLLs

Calling the API from Delphi

Now we are ready to call the NLP API. The process has three steps: generation of JWT, getting an access token and calling the API.

Let’s look at the first step:

JWT generation

So, first of all you need to create a JWT with some claims. A claim is just a “field” with some data in a token, but there are several standard ones like IssuedAt which is implemented as a property. But non-standard claims can also be added. Google requires some claims from us:

Source: manual about authentication for Google APIs

You can get your client email address from your secret JSON file in a field “client_email”:

like this

In this code we generate a token for a half an hour.

At the end, we create a JSON Web signature to sign a token with our RSA key created before using RSA256 algorithm, which is the only algorithm which can be used in authentication for Google API.

Now we have a LCompactedToken of type TJOSEBytes.

Access token request

Let’s look at this step:

We need to post our JWT to a predefined address in an application form format (more at Wikipedia) with two fields: grant_type and assertion with our JWT. Information about this can also be found in a Google authentication manual:

From manual

At the end, we extract a field “access_key” from response and it’s our access key.

Make an API call

Now we can use the API. We got an access token and need to add it into “Authorization” header. And all other things are just parts of request construction. In this case I extract syntax and analyze the sentiment of a sentence “Hello, my name is Elise” and print output to a console. More information about Google Cloud NLP you can find in its documentation.

Here is a screenshot of a demo:

This console application you can find in a GitHub repository and after you add your key as “key.txt” and fill your client email in the code it should work. This demo has some memory leaks and is badly written, but it’s just a demo built from parts of my hackathon code, so don’t copy-paste it :)

I hope, this article will help someone! That’s all for today :)

P.S. If you know that some framework makes it easier, please suggest me in the comments :)

--

--