APIs & Integrations

JohnWongCK
Participant

Using Python script to extract data from HubSpot

SOLVE

Hi,

 

I have written a Python script to extract data from HubSpot using ACCESS TOKEN.

 

I encountered problem on extracting data based on last modified date as shown below.

import requests
from datetime import datetime

dt_str = datetime(2022,10,1,0,0,0).strftime('%Y-%m-%d')

url = 'https://api.hubapi.com/crm/v3/objects/contacts/search'

headers = {'content-type'  : 'application/json',
           'authorization' : 'Bearer %s' % access_token}

data = {'filterGroups' : [{'filters' : [{'propertyName' : 'lastmodifieddate',
                                         'operator'     : 'GTE',
                                         'value'        : dt_str}]}]}

response = requests.request('POST', url, headers=headers, json=data)

print(response)

 

The problem is 

<Response [400]>

 

However, I do not encounter problem of extracting data based on last name and first name as shown below.

import requests

url = 'https://api.hubapi.com/crm/v3/objects/contacts/search'

headers = {'content-type'  : 'application/json',
           'authorization' : 'Bearer %s' % access_token}

data = {'filterGroups' : [{'filters' : [{'propertyName' : 'lastname',
                                         'operator'     : 'EQ',
                                         'value'        : 'Wong'}]}],
        'sorts'        : [{'propertyName' : 'lastmodifieddate',
                           'direction'    : 'DESCENDING'}]}

response = requests.request('POST', url, headers=headers, json=data)

for i in range(10):
    print(response.json()['results'][i])
    print()
import requests

url = 'https://api.hubapi.com/crm/v3/objects/contacts/search'

headers = {'content-type'  : 'application/json',
           'authorization' : 'Bearer %s' % access_token}

data = {'filterGroups' : [{'filters' : [{'propertyName' : 'firstname',
                                         'operator'     : 'EQ',
                                         'value'        : 'John'}]}]}

response = requests.request('POST', url, headers=headers, json=data)

for i in range(5):
    print(response.json()['results'][i])
    print()

 

Hope to get advice on how I can proceed.

 

Thank you very much for any help rendered.

 

John

 

 

1 Accepted solution
ChrisoKlepke
Solution
Key Advisor | Elite Partner
Key Advisor | Elite Partner

Using Python script to extract data from HubSpot

SOLVE

Hey  @JohnWongCK,  

thank you for reaching out to the community. I haven't tested my solution due to time constrains, but I've done similar things in the past. 

 

The problem I suspect here is the datetime variable should be a UNIX timestamp in milliseconds, formatted as a string. So, the result would be, for example:

 

time = "1669902197000"

 

To use the datetime module, you could do something like this for the current time:

 

 currentTime = str(int(datetime.now().timestamp() * 1000))

 

In your case, it should look some like this:

 time = str(int(datetime(2022,10,1,0,0,0).timestamp() * 1000)) 

But again, I haven't tested this yet with your object. 

 

Please let me if this works for you.

 

If you found this post helpful, consider helping others in the community to find answers faster by marking this as a solution. I'd really appreciate it. 

 

Cheers, 

Chriso

 

 

 

View solution in original post

0 Upvotes
3 Replies 3
JohnWongCK
Participant

Using Python script to extract data from HubSpot

SOLVE

Hi @ChrisoKlepke,

 

So sorry for my late reply as I was out of office last Friday.

Yes. I have tested your solution. It worked!

Thank you very much for your great advice.

I will also use your suggested code in my code.

Many thanks once again and have a good day!

 

John

ChrisoKlepke
Key Advisor | Elite Partner
Key Advisor | Elite Partner

Using Python script to extract data from HubSpot

SOLVE

Awesome, @JohnWongCK !

 

Thank you for letting me know if all worked out! Cheers, mate 🎉

ChrisoKlepke
Solution
Key Advisor | Elite Partner
Key Advisor | Elite Partner

Using Python script to extract data from HubSpot

SOLVE

Hey  @JohnWongCK,  

thank you for reaching out to the community. I haven't tested my solution due to time constrains, but I've done similar things in the past. 

 

The problem I suspect here is the datetime variable should be a UNIX timestamp in milliseconds, formatted as a string. So, the result would be, for example:

 

time = "1669902197000"

 

To use the datetime module, you could do something like this for the current time:

 

 currentTime = str(int(datetime.now().timestamp() * 1000))

 

In your case, it should look some like this:

 time = str(int(datetime(2022,10,1,0,0,0).timestamp() * 1000)) 

But again, I haven't tested this yet with your object. 

 

Please let me if this works for you.

 

If you found this post helpful, consider helping others in the community to find answers faster by marking this as a solution. I'd really appreciate it. 

 

Cheers, 

Chriso

 

 

 

0 Upvotes