0

I'm currently using Postman to do an API post request from a CRM software called Intercom. I followed the below documentation to do this:

https://developers.intercom.com/intercom-api-reference/v0/reference/creating-an-export-job

My purpose is to create a script via Google Apps Script to automate the API request.

I need to give the following elements:

  • Method: Post
  • URL: https://api.intercom.io/export/content/data
  • Headers: Authorization : Bearer 123456789, Accept : application/json, Content-Type: application/json
  • Body: "created_at_after": 1654041600, "created_at_before": 1656547200 (this is the date in Unix Timestamp)

The only parameter that will change is the body ("created_at_after" and "created_at_before"). Everything else will remain the same.

Below is the script I've created, that currently does not work. Any help on how to fix this would be appreciated. I'm quite a beginner programmer so apologies in advance if the problem is quite obvious.

function exportjob() {
  var url = 'https://api.intercom.io/export/content/data';
  var options = { 
    "Method": "post",
    "Headers": {
       "Authorization": "Bearer 123456789",
       "Accept": "application/json",
       "Content-Type": "application/json"
       },
    "Body": {
      "created_at_after": 1654041600,
      "created_at_before": 1656547200}
  }
  var response = UrlFetchApp.fetch(url, options);
}
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
Damien
  • 143
  • 1
  • 10

1 Answers1

3

From your showing document, I believe your goal is as follows.

  • You want to convert the following curl command to Google Apps Script.

      curl https://api.intercom.io/export/content/data \
      -X POST \
      -H 'Authorization:Bearer <Your access token>' \
      -H 'Accept: application/json' \
      -H 'Content-Type: application/json' -d '
      {
        "created_at_after": 1527811200,
        "created_at_before": 1530316800
      }'
    

Modification points:

  • At params of fetch(url, params) of Class UrlFetchApp, there are no properties of Method, Headers, Body.
  • In your situation, it seems that it is required to be sent the data as the string. And, by the content type of application/json, the data is parsed at the server side.

When these points are reflected in your script, how about the following modification?

Modified script:

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())
}

Note:

  • If an error occurs for the request, please confirm your access token and the data again. And, please provide the error message.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you so much for your reply, I've got the expected response from Intercom API! Following the documentation, I also need to "Retrieve the job status" and "Retrieve the exported data". Is it possible to do everything on the same script? Or do I need to create a new one? Following the 2 API documentations I mentioned: https://developers.intercom.com/intercom-api-reference/v0/reference/checking-the-status-of-the-job https://developers.intercom.com/intercom-api-reference/v0/reference/downloading-the-data – Damien Jul 19 '22 at 09:39
  • 1
    @Damien Thank you for replying. I'm glad your issue was resolved. About your new 2 questions, unfortunately, I cannot confirm them because I have no account for testing them. I deeply apologize for this. But, it seems that the sample curl commands of your 2 new questions use only headers. So, I think that you can use them by modifying the above sample script. If you cannot do it, please post it as a new question. Because your new 2 questions are different from your this question. If you can cooperate to resolve your new questions, I'm glad. Can you cooperate to do it? – Tanaike Jul 19 '22 at 23:10