APIs & Integrations

DSlavich
Member

Can't use Access Token with Contacts Search API

Hey guys, 

I've been creating an app in Google App Scripts that slings Hubspot data into sheets. So far I've had success using V1 and V2 versions of the API but can't seem to make it work with V3 using the POST method. I keep getting "Authentication credentials not found" despite the fact that my token works when I use Hubspot's built in post test. I would be much obliged if someone could help me with this issue. Below is the relevant script:

var service = getService();
var headers = {headers: {'Authorization': 'Bearer ' + service.getAccessToken()}};;

// array of people with activities
var activePeople = Array();
var keep_going = true;
var offset=0;


while(keep_going){
//Search query
var raw = {"filterGroups":[{"filters":[{"propertyName":"hs_analytics_last_timestamp","operator":"GT","value":"1561514165666"}]}],"properties":["firstname","lastname","email","createdate","hs_analytics_source","hs_analytics_first_touch_converting_campaign","lifecyclestage","hubspotscore","business_segment__c","annualrevenue","monthly_site_visits"],"limit":100,"after":offset}
//Logger.log(raw.after);


//JSON POST Data sent to Hubspot server
var options = {
method : 'post',
contentType: "application/json",
// Convert the JavaScript object to a JSON string.
payload : JSON.stringify(raw),
access_token : headers,
muteHttpExceptions: true
};

// Uses CRM V3's search capability 
var url = API_URL + "/crm/v3/objects/contacts/search?";
var response = UrlFetchApp.fetch(url, options);
var result = JSON.parse(response.getContentText());
const pagination=(Math.floor(result.total/100)*100);

keep_going=JSON.stringify(result.paging);
0 Upvotes
1 Reply 1
DSlavich
Member

Can't use Access Token with Contacts Search API

Figured it out with a good nights sleep. My mistake was syntacticall. I wasn't passing the token properly into the request. Here is the working code for anyone that is interested:

 

 

  // Prepare authentication to Hubspot
   var service = getService();
   var token = service.getAccessToken();

  // array of people with activities
   var activePeople = Array();
   var keep_going = true;
   var offset=0;

   var throttle = 250; // millis
 
   //a loop that runs through the api response, it also updates the pagination for repeat queries 
   while(keep_going){
     //Search query
   var raw = {"filterGroups":[{"filters":[{"propertyName":"hs_analytics_last_timestamp","operator":"GT","value":"1561514165666"}]}],"properties":["firstname","lastname","email","company","createdate","hs_analytics_source","hs_analytics_first_touch_converting_campaign","lifecyclestage","hubspotscore","business_segment__c","annualrevenue","monthly_site_visits"],"limit":100,"after":offset} 
  //Logger.log(raw.after);
   //JSON POST Data sent to Hubspot server
     
       // Uses CRM V3's search capability 
     var url = API_URL + "/crm/v3/objects/contacts/search?";

     var response = UrlFetchApp.fetch(url, {
       method: "POST", 
       payload: JSON.stringify(raw),
       contentType: "application/json",
       headers: {Authorization:'Bearer '+token},
       muteHttpExceptions: true
     });