Workflow: Migrate an API key integration to a private app

We have to change our custom code in workflows (python) from Key to Token.
In my console, the code works fine. Easy to change, but in the workflow I get an Error.
console:

from hubspot import HubSpot
from hubspot.crm.quotes import ApiException

hubspot = HubSpot(access_token=token)
hubspot.crm.quotes.associations_api.get_all(quote_id="56948949", to_object_type="deal", limit=1)​

-> 
{'paging': None, 'results': [{'id': '5529167352', 'type': 'quote_to_deal'}]}

workflow

from hubspot import HubSpot
from hubspot.crm.quotes import ApiException

hubspot = HubSpot(access_token=os.getenv("HTOKEN"))
hubspot.crm.quotes.associations_api.get_all(quote_id="56948949", to_object_type="deal", limit=1)

->
[ERROR] ApiException: (401)
Reason: Unauthorized
HTTP response body: {"status":"error","message":"Authentication credentials not found. This API supports both API Key and OAuth 2.0 authentication and you can find more details at https://developers.hubspot.com/docs/methods/auth/oauth-overview","correlationId":"a166f3d7-1fb9-46f5-a7cf-9fee0787b985","category":"INVALID_AUTHENTICATION"}

Hi @LKlaßen

At the very top of your custom coded workflow can you add this line of code and try again:

import os

Hi @coldrickjack thx for your reply!
Already did this. The hole Test code that don’t work in the Workflow is:

import os
from requests.auth import HTTPBasicAuth
import requests
from hubspot import HubSpot
from hubspot.crm.quotes import ApiException
from hubspot.crm.objects import SimplePublicObjectInput
import json
from datetime import datetime

def main(event):
 htoken = os.getenv("HTOKEN")
 print(type(htoken))
 hubspot = HubSpot(access_token=htoken)
 quote_Id = event.get("inputFields").get("quoteId")
 try:
 api_response = hubspot.crm.quotes.associations_api.get_all(
 quote_id=quote_Id, to_object_type="deal", limit=1)
 except ApiException as e:
 print("Exception when calling associations_api->get_all: %s\n" % e)
 return(api_response)

BUT same works in the console:

import os
from requests.auth import HTTPBasicAuth
import requests
from hubspot import HubSpot
from hubspot.crm.quotes import ApiException
from hubspot.crm.objects import SimplePublicObjectInput
import json
from datetime import datetime

def main():
 htoken = '**********'
 hubspot = HubSpot(access_token=htoken)
 quote_Id = '56948949'
 try:
 api_response = hubspot.crm.quotes.associations_api.get_all(
 quote_id=quote_Id, to_object_type="deal", limit=1)
 except ApiException as e:
 print("Exception when calling associations_api->get_all: %s\n" % e)
 return(api_response)

I can’t see why… with the key it worked fine

Hey @LKlaßen

I think the issue may be that the custom coded workflow actions use an older version of the HubSpot client v4.0.4. Releases · HubSpot/hubspot-api-python · GitHub. Unfortunately there is no way to upgrade the client within a workflow. The only other thing I can think of is not to use the HubSpot client and instead use the Python requests 2.26.0 library. That works for me using below:

import os
import requests
import json

def main(event):

 htoken = os.getenv('HTOKEN')
 quoteId = event.get("inputFields").get("quoteId")
 url = "https://api.hubapi.com/crm/v3/objects/quotes/" + quoteId + "/associations/deal?limit=500"
 headers = {'authorization': 'Bearer ' + htoken }

 r = requests.get(url, headers = headers)
 data = json.loads(r.text)
 print(data)