Articles in this section
Category / Section

Refresh OAuth access token using Data Integration Platform

3 mins read

Introduction:

This article explains how to refresh the OAuth access token to invoke the Rest API when the access token gets expired using the Syncfusion Data Integration Platform.

Steps Involved:

Using the Syncfusion Data Integration Platform:

  1. Save the OAuth access token and its refresh token as a JSON file.
  2. Invoke the Rest API using the access token to fetch data.
  3. Renew the expired access token using the refresh token.
  4. Replace the old JSON (in Step 1) file with the new access token and refresh token.

We have created a sample using the Xero accounting software API that supports OAuth 2.0 authorization.

Data flow in Data Integration Platform

                                                                Data flow in Data Integration Platform

Step 1: Get the credentials file

Save the Xero OAuth access token details in the JSON file as follows,

Credentials file

The GetFile processor is used to get the Xero access token details from the JSON file present in the specified input directory.

GetFile configuration

 

Note:

Schedule the GetFile processor to run for every 10 minutes. Select “CRON driven” in the scheduling strategy of GetFile processor and specify the CRON expression as follows:

CRON scheduling

Step 2: Extract the access token and refresh token

The EvaluateJsonPath processor is used to filter the fields like access token and refresh token from the JSON data and store it as a flow file attribute.

EvaluateJsonPath configuration

Step 3: Invoke Xero API using the access token

The InvokeHttp processor is used to invoke the specified URL. Refer to here to get the API from Xero accounting software and specify the API in the Remote URL property as follows to invoke the Xero API.

InvokeHttp configuration

Add the properties and specify the values for the added properties as follows:

Accept: application/JSON

xero-tenant-id: <your generated Xero tenantId>

xero properties

After adding the required properties, select “true” in the Add Response Headers to Request and Always output Response properties, and click “Apply” as shown in the following image,

InvokeHttp configuration

If the Access token is valid, it returns the data with the status code “200” as shown in the following image,

Status code in attribute

Step 4: Check condition for Authorization

The RouteOnAttribute processor that is used to route flow files based on the attribute's value. It checks the value of the “invoke.status.code” attribute in the incoming flowfile. If it is “Authorized,” the flowfile will be routed to Step 5. If it is “Unauthorized”, the flowfile will be routed to Step 6. Store the status codes as attributes as shown in the following image,

RouteOnAttribute configuration

Step 5: Store the data as a file

The PutFile processor is used to store the incoming flow file in the specified directory. It stores the incoming Xero data as a file in the given directory.

PutFile configuration

Output data in a file:

Output data

Step 6: Replace the Refresh token

The ReplaceText processor is used to update the content of the incoming flow file by evaluating the replacement value. Specify the replacement value as below to replace the refresh token:

Replacement Value: grant_type=refresh_token&refresh_token=${refresh_token}

ReplaceText configuration

 

Step 7: Authenticate using the refresh token

The InvokeHttp processor is used to refresh the access token using the clientid and clientsecret to get the new access token.

Basic Authentication username: <Your ClientId>

Basic Authentication password: <Your ClientSecret>

InvokeHttp configuration

Also, specify the Content-Type property as follows:

InvokeHttp properties

Step 8: Replace the Filename of the incoming flow file

The UpdateAttribute processor is used to update the filename attribute value in the incoming JSON flow file.

UpdateAttribute configuration

Step 9: Replace the file in the same directory

The PutFile processor is used to update the new access token and refreshes token details in a credentials JSON file

PutFile configuration

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied