wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
335 stars 131 forks source link

403 Forbidden when updating a MediaItem I just created? #256

Open audigex opened 3 years ago

audigex commented 3 years ago

I create a MediaItem (uploading an image) ma, then create a post p using WooCommerce. This works fine, both the media item and post are created fine

I then try to update the MediaItem such that mi.Post = p.ID At that point, I get a 403 Forbidden on the call to Media.Update

This happens regardless of whether I use the original returned ma item, or create a new MediaItem and just set Id and Post

Am I doing something wrong, or is this a bug? I don't understand why I would get a 403 when Updating an image when I was just able to successfully create the image

audigex commented 3 years ago

Further details: Here's the request:

{
   "Method":"POST",
   "RequestUri":"https://<redacted>/wp-json/wp/v2/media/140200",
   "Version":1.1,
   "Content":"System.Net.Http.StringContent",
   "Headers":{
      "Authorization":"Bearer <redacted>  Content-Type":"application/json; charset=utf-8  Content-Length":233
   }
}

And response:

{
   "StatusCode":403,
   "ReasonPhrase":"Forbidden",
   "Version":1.1,
   "Content":"System.Net.Http.HttpConnectionResponseContent",
   "Headers":{
      "Date":"Mon",
      "03 May 2021 13":"05":"39 GMT  Server":"Apache  x-backend-server":"<redacted>  x-service-level":"wordpress  x-via":"LHR2  Content-Length":"199  Content-Type":text/html; charset=iso-8859-1
   }
}

And this is all I see in the server access logs (with comments for what I believe is happening at each stage

// Search for the post
[03/May/2021:14:13:36 +0100] "GET /wp-json/wc/v3/products?sku=G-0003-999997 HTTP/1.1" 200 2 "-" "-"
// Upload the media item
[03/May/2021:14:13:38 +0100] "POST /wp-json/wp/v2/media HTTP/1.1" 201 7714 "-" "-"
// I'm pretty sure this is the woocommmerce library retrieving the image I've just created
[03/May/2021:14:13:40 +0100] "GET /wp-content/uploads/2021/05/G-0003-999997.jpg HTTP/1.1" 200 64607 "https://<redacted>/wp-content/uploads/2021/05/G-0003-999997.jpg" "WordPress/5.7.1; https://<redacted>.co.uk"
// Create the product
[03/May/2021:14:13:39 +0100] "POST /wp-json/wc/v3/products HTTP/1.1" 201 6793 "-" "-"
// Attempt to update the media item
[03/May/2021:14:13:41 +0100] "POST /wp-json/wp/v2/media/140203 HTTP/1.1" 403 199 "-" "-"

Is there anything else I can retrieve that would help debug this?

ThomasPe commented 3 years ago

can you link it to a regular (non-Woocommerce) post?

audigex commented 3 years ago

I don't think the site has any non-WooCommerce posts, but I'll see if I can get one created and try it out

NewcodeTechnology commented 2 years ago

Hi I am having a similar issue with uploading media images and I am getting 403 did you get a fix for this issue?