Example Three – Shorten URLs Using Google API

In this example, we describe how to create a data access link to create short URLs using the Google API. This example uses OAuth authentication.

Configure OAuth 2.0 Authentication

To configure authentication:
  1. Sign-up and enable OAuth2.0 for URL Shortener API at https://developers.google.com/apis-explorer/#p/

    From the Google API Manager, note down the client ID, client secret, and refresh token to be used for the data adapter.

  2. From the Navigation menu, browse to Data Adapters > Authentication.

  3. On the List page toolbar, click the New New Button button. 

  4. On the Properties page, on the General tab, Configure OAuth 2.0 Authentication using the client ID, Secret, and refresh token generated from https://developers.google.com. For the rest of the fields, provide the following details:

    1. Token Request URL: Set as https://www.googleapis.com/oauth2/v4/token.

    2. Method: Set as Post.

    3. Headers: Set as Content-Type, application/x-www-form-urlencoded.

    4. Token Request Body: Set as client_id=<client_id>&client_secret=<client_secret>&refresh_token=
      <refresh_token>&grant_type=refresh_token
      .

    5. Access Token JSON Path: Set as $.access_token.

    6. Access Token: Leave this field blank.

  5. Click the Save button.

Create the Access Link

To create a link:
  1. From the Navigation menu, browse to Data Adapters > Access Links.

  2. On the List page toolbar, click the New New Button button. 

  3. On the Properties page, on the General tab, provide the following details.

    • Name: Type the name as Short URLs.

    • Type: Type is set to Web Services - RESTful and cannot be changed.

    • Method: Set the method as Post.

  4. Select the Input tab and provide the following details.

    1. URL: Type the URL https://www.googleapis.com/urlshortener/v1/url.

    2. Authentication: Select the OAuth authentication created for Short URL API.

    3. Accept: Set as application/json.

    4. Content-Type: Set as application/json.

    5. Body: Set the body as {"longUrl": "<%URL%>"}
      The <% URL%> sets up the dynamic variable that needs a value every time this access link is executed.

  5. Select the Output tab, click the New button. The Create Output Format window opens.

  6. In the Create Output Format window, provide the following details:

    1. Extract Or Filter: Select Extract.

    2. Path: Set as $.id.

    3. Internal Field Name: Provide the name as Short URL.

  7. Click the Done button to return to the Output Format page. In the Output Format JSON space, the value of the Field Order field is automatically updated as 1.
  8. Click the Save button.

  9. Click the Test button. The Test Data Access Link window appears.

  10. In the Test Data Access Link window, type any URL.

  11. The Results window then displays the shortened URL returned by the Google API.