Код: Выделить всё
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.tequila.kiwi.com/v2/flights_multi
Вместо этого я попробовал использовать ключ API в качестве параметра (в итоге получил ошибка 400), я попытался удалить два других параметра заголовка (Content-Encoding и Content-Type) и просмотрел параметр «requests» на наличие ошибок. Я также пробовал использовать как json, так и параметры в запросе на публикацию.
Вот документация по коду API текилы, которую я использовал:
Общая информация: https:// tequila.kiwi.com/portal/docs/user_guides/search_api__general_information_
Multicity API: https://tequila.kiwi.com/portal/docs/te ... ticity_api
Вот мой код:
Код: Выделить всё
import requests
from datetime import datetime, timedelta
from monthdelta import monthdelta
import os
# Flight Search endpoint and API key.
FLIGHT_SEARCH_ENDPOINT = "https://api.tequila.kiwi.com/v2/flights_multi"
FLIGHT_SEARCH_API_KEY = os.environ.get("FLIGHT_SEARCH_API_KEY", "Please provide a FLIGHT_SEARCH_API_KEY.")
# City to fly from. We'll use London for this example.
FLY_FROM = "LON"
# Constants for dates tomorrow and six months from tomorrow (our flight date range).
TOMORROW = str((datetime.now() + timedelta(days=1)).strftime("%d/%m/%Y"))
SIX_MONTHS = str((datetime.now() + timedelta(days=1) + monthdelta(6)).strftime("%d/%m/%Y"))
# List of IATA codes for cities to get flight info.
# In order: Paris, Berlin, Tokyo, Sydney, Istanbul, Kuala Lumpur, New York City, San Francisco, Cape Town.
iata_codes = ["PAR", "BER", "TYO", "SYD", "IST", "KUL", "NYC", "SFO", "CPT"]
# This class is responsible for talking to the Flight Search API.
class FlightSearch:
def __init__(self):
self.header = {
"Content-Type": "application/json",
"apikey": FLIGHT_SEARCH_API_KEY,
"Content-Encoding": "gzip"
}
request_list = []
for code in iata_codes:
request_params = {
"fly_to": code,
"fly_from": FLY_FROM,
"date_from": TOMORROW,
"date_to": SIX_MONTHS,
"adults": 1
}
request_list.append(request_params)
self.params = {"requests": request_list}
self.response = requests.post(url=FLIGHT_SEARCH_ENDPOINT, headers=self.header, json=self.params)
self.response.raise_for_status()
print(self.response.text)
flight_search = FlightSearch()
Подробнее здесь: https://stackoverflow.com/questions/783 ... emedy-this