# Upload the image
try:
image_response = requests.post(media_endpoint, headers=image_headers, data=image_data, verify=verify_ssl)
image_response.raise_for_status()
image_id = image_response.json()['id']
if verbose:
print(f"Image uploaded successfully for {card_info['card_title']}. Image ID: {image_id}")
# Set the uploaded image as the featured image of the post
post_update_data = {
'featured_media': image_id
}
try:
update_response = requests.post(f"{card_endpoint}/{card_info['post_id']}", headers=headers, json=post_update_data, verify=verify_ssl)
update_response.raise_for_status()
if verbose:
print(f"Image set as featured for {card_info['card_title']} with ID {image_id} for post ID {card_info['post_id']}.")
except requests.exceptions.HTTPError as http_err:
if update_response.status_code == 403:
print("Permission denied. You are not allowed to use this endpoint or the provided data is not allowed.")
elif update_response.status_code == 404:
print("Endpoint not found. Please check the URL.")
else:
print(f"HTTP error occurred while setting featured image: {http_err}")
except requests.exceptions.ConnectionError:
print("Connection error while setting featured image. Check your internet connection and WordPress URL.")
except requests.exceptions.Timeout:
print("Request timed out while setting featured image. The server might be slow or unresponsive.")
except requests.exceptions.RequestException as err:
print(f"An error occurred while setting featured image: {err}")
Some example code. This is after checking and verifying I can get tokens, and they are up to date.
In this case i’m seeing the errors below: –
HTTP error occurred while setting featured image: 401 Client Error: Unauthorized for url: https://mysite.com/wp-json/wp/v2/card/12767
HTTP error occurred: 401 Client Error: Unauthorized for url: https://mysite.com/wp-json/wp/v2/media/12768
which indicate we can access the site and upload images, but a 401 error is coming back while trying to set the featured image. i’ve checked with various other test scripts and i’m able to authenticate fine.