wp-api-python
wp-api-python copied to clipboard
upload images
Hi
I have tried the code snippet in the readme for upload image with basic auth, but I receive always:
<Response [401]>
{'code': 'rest_cannot_create', 'message': 'Non hai i permessi per creare articoli con questo utente.', 'data': {'status': 401}}
But the user is an admin and it is ok for other apis, I can create posts and categories. Does someone know what can be the problem?
Hey @progressify , try using the demo code in the readme and see how that goes. I've only ever got it working on WP API with basic auth.
Hi @derwentx , ty for reply Yes, I have used your code, but without success.
This is my code:
def upload_wp_image2(self, img):
wpapi = API(
url=self.site_to_sync.url_home,
api="wp-json",
version='wp/v2',
wp_user=self.site_to_sync.username,
wp_pass=self.site_to_sync.password,
basic_auth=True,
user_auth=True,
)
data = open(img.image.path, 'rb').read()
filename = os.path.basename(img.image.path)
_, extension = os.path.splitext(filename)
headers = {
'cache-control': 'no-cache',
'content-type': 'image/{}'.format(extension.replace('.', '')),
'content-disposition': 'attachment; filename={}'.format(filename)
}
return wpapi.post("/media", data, headers=headers)
I don't know whats is wrong. Is probably and apache configuration or a wp setting?
The sample code to upload doesn't work, without the basic auth plugin linked to from here: https://github.com/stylight/python-wordpress-json
Hey @pembo13 and @progressify , I've update the documentation to be more clear that image upload only works with basic auth.
If anyone is still having trouble uploading images with basic auth, try replicating this unit test: https://github.com/derwentx/wp-api-python/blob/b75420c85a6411235dc49bf7bb7cbf82700b2180/tests/test_api.py#L459
If anyone would like to make a fix for this, you're welcome to submit a PR.
Just a reminder to everyone that ends up on this thread: I am no longer regularly maintaining this repo , and for this reason I would advise against people using it until a new maintainer is found
I will be accepting pull requests, and will gladly link to a maintained fork.
Thanks.