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:
-
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.
-
From the Navigation menu, browse to Data Adapters > Authentication.
-
On the List page toolbar, click the New
button.
-
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:
-
Token Request URL: Set as https://www.googleapis.com/oauth2/v4/token.
-
Method: Set as Post.
-
Headers: Set as Content-Type, application/x-www-form-urlencoded.
-
Token Request Body: Set as client_id=<client_id>&client_secret=<client_secret>&refresh_token=
<refresh_token>&grant_type=refresh_token. -
Access Token JSON Path: Set as $.access_token.
-
Access Token: Leave this field blank.
-
-
Click the Save button.
Create the Access Link
To create a link:
-
From the Navigation menu, browse to Data Adapters > Access Links.
-
On the List page toolbar, click the New
button.
-
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.
-
-
Select the Input tab and provide the following details.
-
URL: Type the URL https://www.googleapis.com/urlshortener/v1/url.
-
Authentication: Select the OAuth authentication created for Short URL API.
-
Accept: Set as application/json.
-
Content-Type: Set as application/json.
-
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.
-
-
Select the Output tab, click the New button. The Create Output Format window opens.
-
In the Create Output Format window, provide the following details:
-
Extract Or Filter: Select Extract.
-
Path: Set as $.id.
-
Internal Field Name: Provide the name as Short URL.
-
- 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.
-
Click the Save button.
-
Click the Test button. The Test Data Access Link window appears.
-
In the Test Data Access Link window, type any URL.
-
The Results window then displays the shortened URL returned by the Google API.