Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload images #13

Open
progressify opened this issue Jun 4, 2019 · 4 comments
Open

upload images #13

progressify opened this issue Jun 4, 2019 · 4 comments

Comments

@progressify
Copy link

progressify commented Jun 4, 2019

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?

@d3v-null
Copy link
Owner

d3v-null commented Sep 7, 2019

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.

@progressify
Copy link
Author

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?

@pembo13
Copy link

pembo13 commented May 26, 2020

The sample code to upload doesn't work, without the basic auth plugin linked to from here: https://github.com/stylight/python-wordpress-json

@d3v-null
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants