This post is following my previous one -> How to turn Postman API's request into Apps Script code?
I'm trying to convert 3 API requests into a single piece of code via Google Apps Script. The purpose is to export automatically a set of data from my CRM platform into Google Sheet.
Based on my CRM platform documentation, I need to do 3 API requests in order to get the final set of data I want:
- Creating an export job (using a POST request)
- Retrieve the job status from previous request (using GET request)
- Retrieve the exported data (using GET request)
Below is the code for the 1st API request (kindly provided by Tanaike):
function exportjob() {
var url = 'https://api.intercom.io/export/content/data';
var options = {
"method": "post",
"headers": {
"Authorization": "Bearer 123456789",
"Accept": "application/json",
},
"contentType": "application/json",
"payload": JSON.stringify({
"created_at_after": 1654041600,
"created_at_before": 1656547200
})
}
var response = UrlFetchApp.fetch(url, options);
console.log(response.getContentText())
}
The result of this request is the following
Info {"job_identifier":"5gf58ty4y5y45229r", "status":"pending", "download_url":"", "download_expires_at":""}
Next step is to add the 2 other API requests in my script (Retrieve the job status and retrieve the data). However I have a couple of issues:
For the 2nd API request
- How do I include the job_identifier ID in my code? (given in the 1st request)
For the 3rd API request
- How do I retrieve the data with the URL provided in the 2nd API request?
- The data comes as ZIP file, how do I convert it to CSV so Google Sheet can open it?
- How do get the final set of data to be exported in a google sheet?
Apologies for the long post, I'm trying to summarize the documentation from my CRM platform as much as I can. Any help on how to retrieve the job POST and retrieve the set of data to a google sheet via CSV would be highly appreciated.
Thank you
1. About
For the 2nd API requestIf this is for Retrieve a job status, it seems that the sample curl command is as follows.
It seems that the value of
job_identifiercan be retrieved from your 1st request shown in your question. RefWhen this request is done for the first time, it seems that
statusreturnspending. By this, it seems that untilstatusis changed tocomplete, it is required to wait. Ref2. About
For the 3rd API requestIf this is for Retrieve the exported data, it seems that the sample curl command is as follows.
In this case, the document says
Your exported message data will be streamed continuously back down to you in a gzipped CSV format.. I thought that in this case, the returned data might be able to be ungzipped withUtilities.ungzip, and the ungzipped data might be able to be parsed withUtilities.parseCsv.3. Using your 3 requests, how about the following sample script?
var blob = response3.getBlob().setContentType("application/x-gzip").Note:
When this script is run, the flow of your showing question is done. But, I cannot test this API because I have no account. So, when an error occurs, please check each value and your access token again. And, please provide the error message. By this, I would like to confirm it.
I thought that the value of
download_urlreturned from the 1st request might be the same with the value ofdownload_urlreturned from 2nd request. But, I cannot test this API because I have no account. So, please check it, and when my understanding is correct, you can modify the above script.This sample script creates a new Spreadsheet. But, if you want to put the CSV data to the existing Spreadsheet, please modify the above script.
Reference: