Marketo API: Errors, Warnings, & Results
When you call a Marketo API endpoint, it may return a success response code (200) to Python, even if the call wasn’t successful in Marketo. Therefore, using a try/except block alone won’t catch all errors, and you must check for issues within the JSON response.
Success = False: Smart Campaign Not Found
If you use the Get Smart Campaign by ID endpoint with an invalid ID, the response will include success: false and an error code of '702' with the message 'Smart Campaign not found'.
Success = True But With Warning
When calling the Get Landing Page by ID endpoint, you might see success: true but with a warning message such as 'No assets found for the given search criteria'.
Access Token Expired
Your access token expires after 3600 seconds (1 hour). After that, any API call with the expired token will return a '602' error, indicating 'Access token expired'. Use the OAuth token endpoint to refresh your token.
Checking for Results
A successful API call will have success: true, with no errors or warnings, and the result key will contain the JSON data.
Python Script to Handle Errors and Warnings
The following Python script example demonstrates how to handle errors, warnings, and results by checking the success and warnings values and printing or saving the error details when they occur:
def some_marketo_call():
mat = get_token()
success = False
warnings = False
try:
mkto_call = requests.get(f'https://{mmc}.mktorest.com/rest/asset/v1/smartCampaign/123456.json?access_token={mat}')
mkto_json = mkto_call.json()
print(mkto_json)
success = mkto_json['success']
warnings = True if len(mkto_json['warnings']) > 0 else False
if success and not warnings:
mkto_response = mkto_json['result']
print(mkto_response[0]['id'])
else:
if not success:
if mkto_json['errors'][0]['code'] == '602':
get_token()
else:
error_code = mkto_json['errors'][0]['code']
error_message = mkto_json['errors'][0]['message']
print(f'Error with ID {error_code}: {error_message}')
else:
warning = mkto_json['warnings']
print(f'Warning: {warning}')
except Exception as e:
print(e)