简体   繁体   中英

Make POST API call from Google Sheets to Hubspot with Google Apps Script

I need to create marketing events (like webinar data) in Hubspot taking data from cells in Google Sheets. Marketing events are inside Contacts -> Contacts -> Marketing events. Here is Hubspot API documentation on marketing events https://developers.hubspot.com/docs/api/marketing/marketing-events

I failed to get a request so far.

Here is what I did:

  1. First I created a «private app» under may account and took its token, this is how they do now as API keys are going to be deprecated soon.

For now while I test I allowed all possibles scopes in this app.

  1. I took the number of the account from the URL https://app-eu1.hubspot.com/private-apps/NUMBER (8 digit number)

  2. I tried some endpoints and here might be a mistake, from all documentation it's not obvious which endpoint to take in my case. For now I use this (took it from documentation) var url = 'https://api.hubapi.com/marketing/v3/marketing-events/attendance/NUMBER//email-create';

Then goes my code in Google Apps Script:

//loads data to Hubspot
function loadData(email,eventName,startDateTime,timeEvent,type,eventOrganizer,subscriberState) {

var url = 'https://api.hubapi.com/marketing/v3/marketing-events/attendance/NUMBER//email-create';

var body = {
"email": email,
"eventName": eventName, 
"startDateTime": startDateTime, 
"timeEvent": timeEvent, 
"type": type, 
"eventOrganizer": eventOrganizer, 
"subscriberState":subscriberState
}

var option = {
  "muteHttpExceptions" :true,
  "method":"POST",
  'payload': body,
  "headers": {
      "authorization": ’TOKEN’,
      "content-type": "application/json"
  }
}
var response = UrlFetchApp.fetch(url, option); 
}

Please advise what is wrong here.

From I need to create marketing events (like webinar data) in Hubspot taking data from cells in Google Sheets and your showing request body, when I saw your provided document, it seems that the endpoint is POST /marketing/v3/marketing-events/events which is not POST /marketing/v3/marketing-events/attendance/{externalEventId}/{subscriberState}/email-create . It seems that your endpoint is for

If you wanted to use "Marketing Events", from your provided official document, I found a sample curl command as follows.

curl --request POST \
  --url https://api.hubapi.com/marketing/v3/marketing-events/events \
  --header 'authorization: Bearer YOUR_ACCESS_TOKEN' \
  --header 'content-type: application/json' \
  --data '{
  "eventName": "string",
  "eventType": "string",
  "startDateTime": "2022-12-16T00:51:15.731Z",
  "endDateTime": "2022-12-16T00:51:15.731Z",
  "eventOrganizer": "string",
  "eventDescription": "string",
  "eventUrl": "string",
  "eventCancelled": true,
  "customProperties": [
    {
      "name": "string",
      "value": "string",
      "timestamp": 0,
      "sourceId": "string",
      "sourceLabel": "string",
      "source": "IMPORT",
      "selectedByUser": true,
      "selectedByUserTimestamp": 0,
      "sourceVid": [
        0
      ],
      "sourceMetadata": "string",
      "requestId": "string",
      "updatedByUserId": 0,
      "persistenceTimestamp": 0
    }
  ],
  "externalAccountId": "string",
  "externalEventId": "string"
}'

When this is converted to Google Apps Script, how about modifying your script as follows?

From:

var url = 'https://api.hubapi.com/marketing/v3/marketing-events/attendance/NUMBER//email-create';

To:

var url = 'https://api.hubapi.com/marketing/v3/marketing-events/events';

And also,

From:

var option = {
  "muteHttpExceptions" :true,
  "method":"POST",
  'payload': body,
  "headers": {
      "authorization": ’TOKEN’,
      "content-type": "application/json"
  }
}

To:

var option = {
  "muteHttpExceptions": true,
  "method": "POST",
  "payload": JSON.stringify(body),
  "headers": { "authorization": "Bearer " + "###YOUR_ACCESS_TOKEN###" },
  "contentType": "application/json"
}

Note:

  • If you are required to use your currently showing endpoint of var url = 'https://api.hubapi.com/marketing/v3/marketing-events/attendance/NUMBER//email-create'; , it is required to modify your request body as follows. Please be careful about this.

     curl --request POST \ --url https://api.hubapi.com/marketing/v3/marketing-events/attendance///email-create \ --header 'authorization: Bearer YOUR_ACCESS_TOKEN' \ --header 'content-type: application/json' \ --data '{ "inputs": [ { "interactionDateTime": 0, "properties": { "additionalProp1": "string", "additionalProp2": "string", "additionalProp3": "string" }, "email": "string", "contactProperties": { "additionalProp1": "string", "additionalProp2": "string", "additionalProp3": "string" } } ] }'

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM