This topic was automatically generated from Slack. You can find the original thread here.
I am trying to use the hubspot python api client in a python code step. I am using the magic comments to import it but I am getting this error thrown:
Error
Command failed: python3 -m pip install --target=/tmp/__pdg__/dist/python -r requirements.txt --upgrade WARNING: The directory '/home/sbx_user1051/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag. ERROR: Invalid requirement: 'simplejson, delorean, hubspot-api-client,' (from line 2 of requirements.txt) [notice] A new release of pip is available: 23.0.1 -> 23.3.1 [notice] To update, run: pip install --upgrade pip
Here is my code, it currently just throws the error without running:
# pipedream add-package simplejson, delorean, hubspot-api-client,
import json
from hubspot import HubSpot
from hubspot.crm.contacts import BasicApi, SimplePublicObjectInput
from hubspot.crm.associations import BatchApi, BatchInputPublicAssociationCreate
def handler(pd: Pipedream):
try:
# Get the OAuth token from the connected HubSpot app
token = pd.inputs["hubspot_developer_app"]["$auth"]["oauth_access_token"]
# Initialize the HubSpot client with the OAuth token
api_client = HubSpot(api_key=token)
print("Successfully authenticated with the HubSpot API.")
contacts_api = BasicApi(api_client)
associations_api = BatchApi(api_client)
# Fetch all contacts from HubSpot
existing_contacts = contacts_api.get_all(properties=["linkedin_profile"])
existing_contacts_dict = {contact.properties["linkedin_profile"]: contact.id for contact in existing_contacts.results}
# Extract the LinkedIn profile of the incoming contact
incoming_contact_linkedin = pd.event.body["linkedin_profile"]
# Check if the incoming contact exists
if incoming_contact_linkedin in existing_contacts_dict:
# If the contact exists, update it
contact_id = existing_contacts_dict[incoming_contact_linkedin]
contact_update = SimplePublicObjectInput(properties=pd.event.body)
contacts_api.update_by_id(contact_id, contact_update)
print(f"Updated existing contact with ID {contact_id}")
else:
# If the contact doesn't exist, create it
contact_create = SimplePublicObjectInput(properties=pd.event.body)
created_contact = contacts_api.create(contact_create)
print(f"Created new contact with ID {created_contact.id}")
# Associate the contact with a company
company_id = pd.event.body["hubspot_company_id"]
association_create = BatchInputPublicAssociationCreate(
inputs=[{"from": {"id": created_contact.id}, "to": {"id": company_id}, "type": "contact_to_company"}]
)
associations_api.create_batch(association_create)
print(f"Associated contact with company ID {company_id}")
except Exception as e:
print(f"Error: {e}")
For reference, the hubspot python api is installed using pip like:
pip install --upgrade hubspot-api-client
And used like this:
from hubspot import HubSpot
api_client = HubSpot(access_token='your_access_token')
# or set your access token later
api_client = HubSpot()
api_client.access_token = 'your_access_token'
###
from hubspot.auth.oauth import ApiException
from hubspot.crm.contacts import SimplePublicObjectInputForCreate
from hubspot.crm.contacts.exceptions import ApiException
etc.
What am I am doing wrong here? Please help me figure out this problem, think step by step.