Authorization - access Gmail using OpenAPI client

5 minute read

Overview

This example demonstrates how to configure an API Builder application to work with Gmail. It will walk you through the steps of registering and configuring an application with Google. It will then walk you through setting up API Builder and authorizing it to access the Google application.

We will then create a flow that uses the OpenAPI client plugin to invoke the Gmail API and retrieve the number of the emails received from a provided address.

Creating a new Google application

Before starting on API Builder, you need to register your application on the Google developer console (https://console.developers.google.com/cloud-resource-manager).

To create a project, click Create project.

image2021-8-20_10_12_44

Name your application, “Gmail Project” and click Create.

image2021-8-20_10_13_41

Click Select Project for the new “Gmail Project”, and from the top-left hamburger menu, Navigate to Products > APIs and services > Library.

image2021-8-20_10_14_26

Find and select Gmail API, and then click Enable.

image2021-8-20_10_18_59

Click Create Credentials in the information banner that appears.

image2021-8-20_10_20_49

On the Credential Type screen that appears, in the panel Which API are you using select, “Gmail API”.

image2021-8-20_10_23_12

In the sub-panel, What data will you be accessing, choose “User data”, and then click Next.

image2021-8-20_10_24_23

In the OAuth consent screen, enter your App information, where the App name is “API Builder Gmail”, the User support email is one related to this Google account, and the Developer contact information is the same, and then click Save and Continue (do not click Done yet).

image2021-8-20_10_28_47

You can skip the optional Scopes, click Save and Continue. In the OAuth Client ID screen, choose the Application type, “Web application”.

image2021-8-20_10_31_30

In the Authorized JavaScript origins, add the URL “http://localhost:8080”, and dd an Authorized redirect URI to “http://localhost:8080/auth/callback”, and then click Create.

image2021-8-20_10_34_10

You can then copy or Download your client credentials, and then click Done.

Finally, click on OAuth consent screen from the left menu, and find the Test users section, and add the gmail address of the user that you will use to test this OAuth application. The same user that created the application can be used to test, then click Save.

image2021-8-20_11_12_39

Setting up API Builder

Create an example application:

axway builder init gmail-auth
cd gmail-auth
npm start

Your service is now running and is accessible onhttp://localhost:8080/console.

Import the Gmail OpenAPI specification

  1. We will use the OpenAPI client to connect to the Gmail service. To do this, we can use a simplified Gmail OpenAPI specification is that available here. Download gmail.json and copy it into the ./swagger directory as gmail.json.
  2. Navigate to https://www.iconfinder.com/icons/132837/gmail_icon and save this icon as ./swagger/gmail.png (it is important that the base file names are the same). Note that any of the following image formats can be used: svg, png, gif, bmp, jpg, and tiff.
  3. Restart your service (npm start)
  4. Navigate to the Credentials tab and notice that a Gmail credential has been automatically created. Note that the credential is not ready to be used. For the credential to be ready, we need to provide the client id and client secret that we created above in Creating a new Google application.

image2021-8-20_10_53_22

Configure Gmail credential

For more information on OAuth 2.0 credentials, see OAuth 2.0 credentials.

For Gmail, to get the credential ready, we need to configure the generated credential with our client_id and client_secret. This credential is within a configuration file that was generated by the OpenAPI client plugin when the service was last started. Navigate to the Configuration tab.

image2021-8-20_10_55_41

Select the gmail.default.js configuration file (this is the configuration file that was generated by the OpenAPI client plugin), and enter the Gmail client_id and client_secret that you downloaded previously. image2021-8-20_10_57_43

Click Save and then Proceed on the subsequent modal.

After the service restarts, open the Credentials tab. image2021-8-20_11_0_13 If everything went well, your Gmail Oauth2 credential is now in a state “Requires Authorization”.

Authorize the Gmail credential

Once the Gmail credential is configured, you need to manually authorize the credential to use it. Click on the Credentials tab and click Authorize. You will be prompted to choose a Google account or log in. The account must also have the Gmail service enabled.

image2021-8-20_11_6_5

You may see a screen informing you that Google has not yet verified the application. That can be done from the Google console at a later stage when you publish your application, for now, click Continue, and then click Allow to approve the scope(s) to access to the account.

image2021-8-20_11_15_3 The credential is now “green”, and ready to use.

image2021-8-20_11_17_29

Create a flow to count your emails

In API Builder, create a new flow by clicking Flows and then the add Flow button in the upper-right. This will bring you into the flow editor.

image2021-8-20_11_20_52

Drag and drop the Gmail icon from the Flow-Nodes panel on the left into the graph on the right.

image2021-8-20_11_22_7

In property panel on the right, change the Oauth2 input to “Credential” and use the “Gmail Oauth2” credential. Then, change the userId to String, and enter “me”. It should look like the image below.

image2021-8-20_11_24_28

Then click the debug icon in the upper-right of the flow graph, and then click Execute Flow.

image2021-8-20_11_26_32

All going well, you should see a list of message identifiers retrieved from the Gmail service in the debug panel, and in the console.

image2021-8-20_11_27_46

This was a simple integration between API Builder and Gmail to access the list of contents. You can use a similar approach to work with any API; all you need to provide is the OpenAPI specification describing the API and API Builder can integrate with it seamlessly.

Last modified March 25, 2022: Johannesburg Release Notes (#70) (92613bd)