R
R
Roman2021-07-09 12:50:29
Python
Roman, 2021-07-09 12:50:29

How to leave a response on Yandex Services via requests?

I am making a bot that would allow me to respond to tasks on Yandex services. I use Python and Requests. I pass authorization, I load pages. The problem arises when I want to leave a response to a task through the API. When trying to do this, Yandex returns a 403 error and undefined with the headers:

{'Cache-Control': 'no-cache, max-age=0, must-revalidate, no-store', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'X-Date': '1625834781', 'X-Yandex-Req-Id': '1625834781657609-865179455691553378100330-man2-1242-man-shared-app-host-20030', 'x-app-outdated-status': 'false', 'x-auth-error': '2'}


'x-auth-error': '2' I understand that the error is in the authorization, but I don't understand what exactly is wrong.

Source:

# -*- coding: utf-8 -*-
import json
import pickle
import requests

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
           'Content-Type': 'application/json;charset=utf-8',
           'Connection': 'keep-alive'}
login = ''
password = ''

api_link = 'https://uslugi.yandex.ru/api/orders?completely_moderated=0&region=213&region=1&rubric=%2Fkomp_utery-i-it%2Fkomp_uternaa-pomos_&rubric=%2Fremont-i-ustanovka-tehniki%2Fposudomoecnye-masiny&rubric=%2Fremont-i-ustanovka-tehniki%2Fstiral_nye-masiny&rubric=%2Fremont-i-stroitel_stvo%2Felektromontajnye-raboty&rubric=%2Fremont-i-stroitel_stvo%2Fmaster-na-cas&rubric=%2Fremont-i-stroitel_stvo%2Fsantehniceskie-raboty-i-otoplenie&type=new&with_suggested_to_me=false'


class Yandex:

    session = requests.Session()

    def auth(self):
        """Авторизация на Яндексе"""
        start_page = self.session.post('https://passport.yandex.ru/auth', data={'login': login, 'passwd': password}, headers=headers)
        print(f'Авторизация {start_page}')

    def save_cookies(self):
        """Сохранение Cookies"""
        with open('cookies', 'wb') as file:
            pickle.dump(self.session.cookies, file)
        print('Cookies сохранены')

    def load_cookies(self):
        """Загрузка Cookies"""
        with open('cookies', 'rb') as file:
            self.session.cookies.update(pickle.load(file))
        print('Cookies загружены')

    def api_page(self):
        """Страница со списком заданий"""
        uslugi = self.session.get(api_link, headers=headers)
        print(uslugi.status_code)
        print('')
        print(uslugi.json())

    def create_order_reaction(self):
        """Создание отклика"""
        data = {
            "data": {
                "params": {
                    "order_id": "a2ffdd24-de65-401f-a2f2-75d8b77941e9",
                    "order_reaction_info": {
                        "text": "Обращайтесь",
                        "desired_datetime": None,
                        "price": "500",
                        "photo_urls": []
                    }}}}

        order_page = self.session.post('https://uslugi.yandex.ru/api/create_order_reaction', json=data, headers=headers)
        print(order_page.status_code)
        print(order_page.text)
        print(order_page.headers)


yandex = Yandex()
yandex.load_cookies()
yandex.create_order_reaction()


Here are the requests that the browser sends and receives:

60e81b74be11a629956158.jpeg
60e81b7f4e65f085457567.jpeg

I tried to substitute different headers, but still nothing happens. Tell me, please, where could be the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-07-09
@cittadella

At least csrf token is missing in headers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question