Updating Google Apps Script to fetch HubSpot data using Access Token

I’m trying to pull HubSpot info into Google Sheets but I’m not great at coding. I found a tutorial using Google Apps Script, but it’s outdated because HubSpot stopped using API keys. Now I’m stuck.

Here’s what I’ve done so far:

function getHubSpotContacts() {
  var endpoint = 'https://api.hubapi.com/crm/v3/objects/contacts?limit=10&archived=false';
  var accessToken = 'YOUR_ACCESS_TOKEN_HERE';

  var options = {
    'headers': {
      'Authorization': 'Bearer ' + accessToken
    },
    'method': 'get'
  };

  var response = UrlFetchApp.fetch(endpoint, options);
  var data = JSON.parse(response.getContentText());
  
  // Process and display data in spreadsheet
  // (code for this part omitted for brevity)
}

I changed the URL and added the access token, but I’m not sure if it’s correct. Can someone help me fix this script so it works with the new HubSpot API authentication method? Thanks!

Hey there! I’ve been in your shoes, struggling with API changes. Your script looks pretty close, actually. I’ve had success using a similar approach for HubSpot recently.

A couple of things to double-check:

Make sure your access token is valid and hasn’t expired. HubSpot tokens typically last for 6 hours.

You might want to add some error handling. Sometimes the API can be finicky, and it helps to know what’s going wrong. Try wrapping your UrlFetchApp.fetch in a try-catch block.

Also, don’t forget to set up the proper scopes when you’re generating your access token. For contacts, you’ll need at least ‘contacts’ scope.

If you’re still having trouble, HubSpot’s developer forums are a goldmine. They’ve helped me out of many sticky situations with their API.

Good luck with your project! API work can be frustrating, but it’s so satisfying when you finally get it working.