rest api – 403 Forbidden when Updating (I presume POST?) but not Creating (PUT?) an image
I’m using the WordPressPCL library for .NET alongside WooCommerce (although my problem is with the WordPress API, not the WooCommerce API), and trying to upload an image alongside my product
My current process is
- Upload the image using WordPressPCL, using the Media.Create() method which I presume PUTs the image
- Create the product using WooCommerceNET, with the image’s URL as the product URL
- Update the image, setting the Media’s “Post” ID to the WooCommerce product ID
Steps 1 and 2 work fine and I can upload dozens of products, but on 3 I get a 403 Forbidden response which doesn’t make sense to me since I was able to upload the image.
The above order is important to me, because I absolutely do not want to create a product without the image already being on the server – therefore I need to guarantee the process fails if there’s a problem with either the image upload or the product upload. Hence I update the image to attach to the post afterwards.
I know that you don’t strictly need to attach the image to a post, but it’s a requirement from the customer, who uses the attachment to manage deletion of images.
Simplified code below (I’ve removed the details of error handling, object creation etc)
// Create the image
try
{
uploadedImage = await wordPressClient.Media.Create(imagePath, fileName);
}
catch
{
// Image upload failed. Fail the process
}
// Image uploaded successfully, now create the product
try
{
productImage = new ProductImage()
{
src = uploadedImage.SourceUrl;
};
product.images.Add(productImage);
// Replace with the product recieved from the server so that
product = await wooCommerceClient.Product.Add(product);
}
catch(Exception e)
{
// Product creation failed, fail the process (and try to remove the image, but don't worry about it)
}
// Now try to link the image to the post
try
{
uploadedImage.Post = (int)product.id;
await wordPressClient.Media.Update(uploadedImage);
}
catch
{
// Problem here
}
Is there some kind of API permission I need to set to allow my API user to update the data for an existing media item? Or something else I’m missing?
Leave an answer