-
-
Notifications
You must be signed in to change notification settings - Fork 134
Description
I am having an issue uploading media using this library.
Using the below code, I have successfully added and removed media from two different wordpress sites. Works well. But the 3rd site, I get the below exception.
Given the style of exception message, I was fairly sure it was not the original problem. After some digging, it turns out the response from WordPress is 403 Forbidden.
I can post media using Postman so I know authentication is working on WordPress.
What can I do to get a more coherent message and avoid this exception? And figure out why Wordpress would be forbidding posts from this library on this one specific site?
try
{
var client = SetClientInfo(webstore);
return await client.Media.CreateAsync(file.FullName, file.Name);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to upload an image to the WordPress Media Library");
}
private WordPressClient SetClientInfo(WooCommerceInstance webstore)
{
var httpClient = new HttpClient
{
BaseAddress = new Uri(webstore.BaseUrl + "/wp-json/")
};
var v = System.Reflection.Assembly.GetEntryAssembly()!.GetName().Version;
httpClient.DefaultRequestHeaders.Add("User-Agent", $"<redacted>" + '/' + v.Major.ToString() + '.' + v.Minor.ToString() + "." + v.Build.ToString());
var wordPressClient = new WordPressClient(httpClient);
wordPressClient.Auth.UseBasicAuth(webstore.WordPressUserName, webstore.WordPressAppPassword);
return wordPressClient;
}
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WordPressPCL.Models.BadRequest' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.