Refresh OAuth access token using Data Integration Platform
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:
- Save the OAuth access token and its refresh token as a JSON file.
- Invoke the Rest API using the access token to fetch data.
- Renew the expired access token using the refresh token.
- 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
Step 1: Get the credentials file
Save the Xero OAuth access token details in the JSON file as follows,
The GetFile processor is used to get the Xero access token details from the JSON file present in the specified input directory.
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:
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.
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.
Add the properties and specify the values for the added properties as follows:
Accept: application/JSON
xero-tenant-id: <your generated Xero tenantId>
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,
If the Access token is valid, it returns the data with the status code “200” as shown in the following image,
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,
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.
Output data in a file:
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}
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>
Also, specify the Content-Type property as follows:
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.
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