Hi,
I have a problem. I have an API program that accesses the HubSpot file system and returns some replies. It’s a big reply, so I try to use pagination. Sadly when i request a pagination token it comes as a string, in this case, “AAAACg”. When it then follows to page 2 it gives an error. It takes the first 10 objects it gets and successfully inserts it into Google Sheets, and when it wants to get page 2, it stops. The program is in node.js and i run on my VS Code Studio. What could be the problem? I’ve spent days and even hired 14 different upwork freelancers. Nobody was able to move it.
I wanted to contact Slack HS Developers, but the Slack seems to be turned off.
The error this.
Google Sheets authenticated!
HubSpot API Request Parameters:
{ after: undefined }
Next 'after' token: AAAACg
Type of 'after' token before conversions: string
Decoded 'after' token: NaN
Data saved to Google Sheets!
Error fetching items: HTTP-Code: 400
Message: An error occurred.
Body: {"status":"error","message":"Invalid input JSON on line -1, column -1: Cannot deserialize value of type `long` from String \"AAAACg\": not a valid `long` value","correlationId":"e2f4e619-0883-4174-89ac-5f347e01321a"}
Headers: {"access-control-allow-credentials":"false","cf-cache-status":"DYNAMIC","cf-ray":"8fa200e40a88b391-PRG","connection":"keep-alive","content-length":"216","content-type":"application/json;charset=utf-8","date":"Mon, 30 Dec 2024 12:23:51 GMT","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=rH9fhc3xl5oTsShl9DBuI26AZMxM48fWHFvPFBaU1Yfbr8dA0TFqiyeeq6ABnQMFsUwlw9kHgd8xatbDCst4VrPq0WsHKvqn08aUzMqDdtYC0P1pfd9Nle2CCdzX6ENs\"}],\"group\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","set-cookie":"__cf_bm=o3oVSzuDJGqTZi.n5NNJgRzHxNReqI27loqAA1dRt5E-1735561431-1.0.1.1-n2cCtOuB0r_IoO_ZLQ_0VICD8tl92_MLhLrEssigvWE5rNYOSKqfjlOImn.KRGRYfoTUUGkRbcNKptG4dAQl7A; path=/; expires=Mon, 30-Dec-24 12:53:51 GMT; domain=.hubapi.com; HttpOnly; Secure; SameSite=None","strict-transport-security":"max-age=31536000; includeSubDomains; preload","vary":"origin, Accept-Encoding","x-content-type-options":"nosniff","x-hubspot-correlation-id":"e2f4e619-0883-4174-89ac-5f347e01321a","x-hubspot-ratelimit-daily":"625000","x-hubspot-ratelimit-daily-remaining":"622290","x-hubspot-ratelimit-interval-milliseconds":"10000","x-hubspot-ratelimit-max":"190","x-hubspot-ratelimit-remaining":"188","x-hubspot-ratelimit-secondly":"19","x-hubspot-ratelimit-secondly-remaining":"18"}
API Response Body: No response body
Fetched 10 items in total.
My code is this.
require('dotenv').config(); // Load .env file
const { google } = require("googleapis");
const hubspot = require('@hubspot/api-client');
// Initialize the HubSpot client with the token from .env
const hubspotClient = new hubspot.Client({
accessToken: process.env.HUBSPOT_API_TOKEN,
});
// Authenticate Google Sheets API
async function authenticateGoogleSheets() {
const auth = new google.auth.GoogleAuth({
keyFile: "credentials.json", // Path to your service account key file
scopes: ["https://www.googleapis.com/auth/spreadsheets"],
});
return google.sheets({ version: "v4", auth });
}
async function appendToGoogleSheet(sheets, spreadsheetId, range, values) {
try {
const result = await sheets.spreadsheets.values.append({
spreadsheetId,
range,
valueInputOption: "USER_ENTERED",
resource: { values },
});
console.log("Data saved to Google Sheets!");
return result;
} catch (error) {
console.error("Error appending to Google Sheet:", error.message);
throw error;
}
}
// Function to convert bytes to kB or MB
function formatSize(bytes) {
if (bytes < 1024) {
return `${bytes} B`; // Bytes
} else if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(2)} kB`; // Kilobytes
} else {
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`; // Megabytes
}
}
async function fetchAllItems(parentFolderId = undefined, sheets, spreadsheetId) {
const results = [];
let after = undefined; // Pagination cursor
do {
try {
// Call HubSpot API to search files and folders
const apiResponse = await hubspotClient.files.filesApi.doSearch(
undefined, // name
undefined, // path
undefined, // url
undefined, // type
undefined, // size
after // Pagination cursor
);
// Log the request details
console.log("HubSpot API Request Parameters:");
console.log({
after: after, // Pagination token
});
const items = apiResponse.results;
after = apiResponse.paging?.next?.after; // Keep the token as a string or undefined
console.log(apiResponse);
// Debugging: Log the `after` value and its type
console.log("Next 'after' token:", after);
console.log("Type of 'after' token before conversions:", typeof after);
if (!after) {
console.log('No more items to fetch.');
break;
}
// Process each item and extract only the desired properties
const filteredItems = items.map((item) => [
item.name,
item.path,
item.url,
item.type,
item.size ? formatSize(item.size) : "N/A" // Convert size to kB or MB
]);
// Append filtered items to Google Sheets in Tab '20'
await appendToGoogleSheet(sheets, spreadsheetId, "20!A2", filteredItems);
for (const item of items) {
results.push(item);
// If the item is a folder, fetch its contents recursively
if (item.type === 'FOLDER') {
const subItems = await fetchAllItems(item.id, sheets, spreadsheetId);
results.push(...subItems);
}
}
} catch (error) {
console.error("Error fetching items:", error.message);
console.error("API Response Body:", error.response?.data || "No response body");
break; // Stop further processing on error
}
} while (after); // Continue fetching until all pages are retrieved
return results;
}
// Main function to fetch all items starting from the root
(async () => {
if (!process.env.HUBSPOT_API_TOKEN) {
console.error("HUBSPOT_API_TOKEN is missing. Check your .env file.");
process.exit(1); // Exit if token is not set
}
const spreadsheetId = "1J6lzh42NB7quPuaUUkdj-W8yFAsuaaGnSQNXQ8P_DNk"; // Google Sheets ID
try {
const sheets = await authenticateGoogleSheets(); // Authenticate Sheets API
console.log("Google Sheets authenticated!");
const allItems = await fetchAllItems(undefined, sheets, spreadsheetId); // Start from the root folder
console.log(`Fetched ${allItems.length} items in total.`);
} catch (error) {
console.error("Error fetching items:", error.message);
}
})();
I think the issue is that it returns a string, which by GitHub documentation i found is what we should expect, but when sending it back for page 2 it says it wants long. And I wasn’t able to change it from string to long. What do I do?