Невозможно загрузить медиафайл с помощью Wordpress API (через Python) - PullRequest
0 голосов
/ 02 декабря 2018

Я пытаюсь загрузить файлы в медиатеку WordPress через Python, но получаю сообщение об ошибке, как показано ниже.

I am getting below error { 'code': 'rest_cannot_create', 'data': {'status': 401}, 'message': 'Sorry, you are not allowed to create posts as this user.'}

Я установил Basic Auth в WordPress и включил его.Вот мой код:

import sys
import os
import ntpath
import getpass
import requests
import pprint

url='http://rparthiban.com/articles/wp-json/wp/v2/media'


import csv
result_paths = []

user = input('Username:')
password = getpass.getpass('Password:')


session = requests.Session()
with open('list_html.csv') as csvfile:
    ipynb_reader = csv.reader(csvfile)
    for each_row in ipynb_reader:
        current_file =  each_row[0]
        # print(current_file)

        data = open(current_file, 'rb').read()
        fileName = os.path.basename(current_file)

        res = requests.post(url,
                            data=data,
                            headers={ 'User-Agent': 'Mozilla/5.0', 'Content-Type': '','Content-Disposition' : 'attachment; filename=%s'% fileName},
                            auth=(user, password))
        pp = pprint.PrettyPrinter(indent=4) ## print it pretty. 
        pp.pprint(res.json()) #this is nice when you need it
        # newDict=res.json()
        # newID= newDict.get('id')
        # link = newDict.get('guid').get("rendered")
        # print(newID, link)

Ссылка здесь , говорит о nonce, который будет предоставлен, но я понятия не имею, как генерировать и предоставлять здесь (я нуб в webdev).Пожалуйста, помогите.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...