'API can't find my API key in header even though i included it
I'm trying to use the tequila API to retrieve flight prices but the server keeps returning this error:
Traceback (most recent call last):
File "C:\Users\Senna\Downloads\flight-deals-step-2-solution\flight-deals-step-2-solution\main.py", line 37, in <module>
flight = flight_search.check_flights(
File "C:\Users\Senna\Downloads\flight-deals-step-2-solution\flight-deals-step-2-solution\flight_search.py", line 54, in check_flights
response.raise_for_status()
File "C:\Users\Senna\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 953, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://tequila-api.kiwi.com/v2/search?fly_from=AMS&fly_to=CDG&date_from=15%2F08%2F2021&date_to=10%2F02%2F2022&nights_in_dst_from=7&nights_in_dst_to=28&flight_type=round&one_for_city=1&max_stopovers=0&curr=GBP
Process finished with exit code 1
this is my code: (method in class)
def check_flights(self, origin_city_code, destination_city_code, from_time, to_time):
headers = {"apikey": TEQUILA_API_KEY}
query = {
"fly_from": origin_city_code,
"fly_to": destination_city_code,
"date_from": from_time.strftime("%d/%m/%Y"),
"date_to": to_time.strftime("%d/%m/%Y"),
"nights_in_dst_from": 7,
"nights_in_dst_to": 28,
"flight_type": "round",
"one_for_city": 1,
"max_stopovers": 0,
"curr": "GBP"
}
response = requests.get(
url="https://tequila-api.kiwi.com/v2/search",
headers=headers,
params=query,
)
response.raise_for_status()
try:
data = response.json()["data"][0]
except IndexError:
print(f"No flights found for {destination_city_code}.")
return None
flight_data = FlightData(
price=data["price"],
origin_city=data["route"][0]["cityFrom"],
origin_airport=data["route"][0]["flyFrom"],
destination_city=data["route"][0]["cityTo"],
destination_airport=data["route"][0]["flyTo"],
out_date=data["route"][0]["local_departure"].split("T")[0],
return_date=data["route"][1]["local_departure"].split("T")[0]
)
print(f"{flight_data.destination_city}: £{flight_data.price}")
return flight_data
Solution 1:[1]
Is this from the angela yu's python bootcamp i'm having problems with the same project in my case I am able to get the data but the origin_location changes
eg: if i have set the origin_city i.e the city from where i want to board the flight as bombay and the destination_city i.e the city where i want to get down at as paris . When i run the code the origin_city automatically changes to manchester for some reason
Solution 2:[2]
Im also doing the course and got absolutely trainwrecked on the header not being recognized. After 2 hours of trying every possible solution to make the header reach the API I recognized that I forgot to format date_to in the correct string format. So despite the API is giving me header not found the issue was with one of my parameters. So just give it another go and check your code character by character against the API documentation and I am sure you will find a tiny bit that has a typo or wrong format and your request will eventually go through.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Abhishek |
Solution 2 | tsongi |