D
D
domanskiy2021-07-06 08:33:03
Python
domanskiy, 2021-07-06 08:33:03

How to upload JPG PNG to WordPress gallery via REST API using Python and requests?

My code

import base64
import json
import os

import requests

# Авторизация. Плагин Application Passwords https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide
# лог/пас сгенерированы в меню Users -> учетка админа -> Пароли приложений
USER = 'wp_api_python'
PASSWORD = '3KDD 91Bh y2Gl qt9H clgZ GEAl'

img_path = "Render_Project_Champery_002.png"

url = "http://suite.ru/wp-json/wp/v2/media"
creds = USER + ':' + PASSWORD

token = base64.b64encode(creds.encode())

header = {
    'Authorization': 'Basic ' + token.decode('utf-8'),
    "Accept": "application/json",
}

media = {
    'file': open(img_path, 'rb'),
    'caption': 'First TEST API Image upload',
    'description': 'First TEST API Image upload'
}

image = requests.post(url=url, headers=header, files=media)
print(image.json())

Answer:
{'code': 'rest_cannot_create', 'message': 'Извините, вам не разрешено создавать записи от лица этого пользователя.', 'data': {'status': 401}}


I don't understand what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexBoss, 2021-07-06
@AlexBoss

401 code - not authorized

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question