I am trying to extract data from hubspot deals using the access token given by my mentor. Blow is my code for the extraction,
import requests
import json
import urllib
import csv
# Set your variables
max_results = 3100
access_token = 'my access token'
limit = 100
deal_list = []
get_all_deals_url = "https://api.hubapi.com/crm/v3/objects/deals"
# Set up the headers, including the OAuth 2.0 access token
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# Set up the initial parameters
parameter_dict = {
'limit': limit,
}
while True:
# Make the API request
response = requests.get(get_all_deals_url, headers=headers, params=parameter_dict)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Print the first deal data to inspect the structure
if data['results']:
print(json.dumps(data['results'][0], indent=4))
deal_list.extend(data['results'])
# Check if there are more pages
if data.get('paging', {}).get('next', {}).get('after'):
parameter_dict['after'] = data['paging']['next']['after']
else:
break
else:
# If the request was not successful, print the error
print(f"Failed to retrieve deals: {response.status_code}")
print(response.text)
break
# Extract the necessary properties and write to a CSV
with open('deals.csv', 'w', newline='', encoding='utf-8') as output_file:
writer = csv.writer(output_file)
writer.writerow(['id', 'created_at', 'updated_at', 'deal_name', 'is_archived']) # Header
for deal in deal_list:
deal_id = deal.get('id')
created_at = deal.get('createdAt')
updated_at = deal.get('updatedAt')
deal_name = deal.get('properties', {}).get('dealname')
is_archived = deal.get('archived')
writer.writerow([deal_id, created_at, updated_at, deal_name, is_archived])
here is the output that I have,
{
“id”: “…”,
“properties”: {
“amount”: “4342”,
“closedate”: “2019-10-22T06:00:00Z”,
“createdate”: “2018-10-04T07:00:00Z”,
“dealname”: “A Good Timeout - New Deal”,
“dealstage”: “closedlost”,
“hs_lastmodifieddate”: “2019-10-09T06:15:20.930Z”,
“hs_object_id”: “…”,
“pipeline”: “default”
},
“createdAt”: “2020-10-05T07:00:00Z”,
“updatedAt”: “2023-09-06T06:15:20.930Z”,
“archived”: false
}
the issue that I have is that it does not return the source of a deal property named ‘billing effective date’.