Google Apps Script: How to connect to HubSpot API with Bearer token after API key deprecation

Need help updating my Google Apps Script for HubSpot integration

I’m working on pulling HubSpot contact data into Google Sheets using Apps Script. I’m not really a programmer but I found some old code that used to work. The problem is HubSpot stopped supporting API keys and now I need to use access tokens instead.

I tried updating the code but I think I’m doing something wrong with the authorization part. Can someone help me fix this?

Here’s what I’m trying to make work:

function fetchHubspotData() {
  var ACCESS_TOKEN = "YOUR_TOKEN_HERE";
  
  var apiUrl = "https://api.hubapi.com/crm/v3/objects/contacts?limit=15&archived=false";
  
  var requestOptions = {
    "method": "GET",
    "headers": {
      "Authorization": "Bearer " + ACCESS_TOKEN,
      "Content-Type": "application/json"
    }
  };
  
  var apiResponse = UrlFetchApp.fetch(apiUrl, requestOptions);
  var jsonData = JSON.parse(apiResponse.getContentText());
  var contactList = jsonData['results'];
  
  var currentSheet = SpreadsheetApp.getActiveSheet();
  var columnHeaders = ["Business", "Created", "Email Address", "Given Name", "Updated", "Family Name", "Mobile", "Domain"];
  var dataRows = [columnHeaders];
  
  contactList.forEach(function (contact) {
    dataRows.push([
      contact['properties'].company,
      contact['properties'].createdate,
      contact['properties'].email,
      contact['properties'].firstname,
      contact['properties'].lastmodifieddate,
      contact['properties'].lastname,
      contact['properties'].phone,
      contact['properties'].website
    ]);
  });
  
  currentSheet.getRange(1, 1, dataRows.length, dataRows[0].length).setValues(dataRows);
}

The script runs but I get authentication errors. What am I missing here?

I encountered a similar issue when HubSpot deprecated API keys. Your approach seems correct, but it’s crucial to use a private app access token instead of OAuth. Private apps simplify the process as they eliminate the need for OAuth authentication. To resolve this, navigate to HubSpot settings, create a private app, ensure it has the appropriate contacts read permission, and obtain the access token. Additionally, implement error handling in your UrlFetchApp call to gracefully manage any exceptions from HubSpot’s API, checking the response code before parsing JSON to avoid potential issues.

Had this exact same issue when switching from API keys to access tokens. Your code looks right, but check if your access token has any extra spaces or line breaks when you copied it. I wasted hours on that stupid mistake. Also, sandbox tokens can be formatted differently than production ones. I’d test the same headers and URL in Postman or similar first - that way you’ll know if it’s a HubSpot auth problem or Apps Script being weird. The error response usually tells you exactly what’s wrong with your token or permissions.

Had this exact problem last month - it’s a token timing issue. If you created your access token before HubSpot finished rolling out the API key deprecation, those tokens get stuck in limbo. They’ll authenticate but won’t actually work. I fixed it by deleting the entire private app and creating a brand new one with fresh credentials. HubSpot can take up to 30 minutes to activate new private app tokens on their end, even though they show as active right away. Generate a new token and wait a bit before testing your script.

Check if your HubSpot app permissions match what you’re accessing. The token might work but lack contacts scope. Also add muteHttpExceptions: true to your requestOptions - Apps Script gets picky with HTTP responses and this helps debug what’s going wrong.

The authentication looks right, but here’s a gotcha that bit me - HubSpot throws different error messages based on how you created your access token. If you’re using a legacy developer account token, it might’ve gotten hit by the API key deprecation even though it’s technically an access token. Create a brand new private app from scratch in your HubSpot developer settings and generate a fresh token. Also check if your HubSpot account tier actually supports v3 API endpoints - some older accounts have restrictions that aren’t obvious until you switch auth methods. Those properties should be available by default, but make sure none need special permissions in your app config.

your code looks good! But make sure to replace “YOUR_TOKEN_HERE” with your actual HubSpot access token. Also, keep an eye on the token expiration - they don’t last forever, so you might need a fresh one.

Your authentication setup looks right. Here’s what’s probably going wrong:

First, check your access token has the right scopes - you need at least crm.objects.contacts.read for this to work. HubSpot tokens also expire, so test yours in Postman first to make sure it’s still valid.

Another thing - watch out for extra spaces or characters when you copy/paste the token into your script. That trips people up all the time.

If you’re still getting auth errors, log the actual response to see what HubSpot’s sending back. Add console.log(apiResponse.getContentText()) before your JSON parsing line.