401 Error when using NetSuite SuiteScript RESTlet API call to HubSpot to collect contact info

59 Views Asked by At

I have built a custom API from NetSuite to HubSpot. The API needs to read all contats from HubSpot and cross-reference them in NetSuite. Any new contacts that are not in NetSuite need to be added and the contact info in HubSpot needs to be filled in a new record form. The issue I'm experiencing is the connection to HubSpot. The program runs and I receive a 401 error code: INVALID_LOGIN_ATTEMPT.

I use the same API url and key with Postman, and I am able to authenticate and pull records from my account.

I believe the issue is the SuiteScript syntax which I am not familiar with. Any help is gretly appreciated!

See code below.

    // HubSpot account ID
    const hubspotAccountId = 'TOKEN_HIDDEN';

    // Specify the number of contacts to return in your API call. The default is 20, maximum is 100
    const count = 100;

    async function getHubSpotContacts(offset = 0) {
        const hapikeyParam = `hapikey=${hubspotAccountId}`;
        const paramsString = `?count=${count}&${hapikeyParam}&vidOffset=${offset}`;

        const finalUrl = `https://api.hubapi.com/contacts/v1/lists/all/contacts/all${paramsString}`;

        const response = await https.get({
            url: finalUrl
        });

        if (response.body) {
            return JSON.parse(response.body);
        } else {
            throw new Error('Failed to fetch HubSpot contacts');
        }
    }

I have tried 7 different syntax for the GET async function: getHubSpotContacts.

The function needs to connect to HubSpot and collect Contact information.

Running the API in NetSuite produces a 401 error code.

Running the same URL and header in Postman is successful.

Another example that failed:

    async function getHubSpotContacts() {
        try {
            const hubSpotContacts = await https.get({
                    url: `https://api.hubapi.com/contacts/v1/lists/all/contacts/all`,
                    headers: {
                        'accept': 'application/json',
                        'Authorization': 'Bearer TOKEN_HIDDEN', 
                    }  

        });
0

There are 0 best solutions below