I keep getting KeyError: 'be25d95e' (lecture 333, of 100 days of code by Dr. Angela Yu)

Question:

I downloaded the solution code
(for lecture 333, of 100 days of code by Dr. Angela Yu)
but for some reason, it just generates this Key error.

Code:

import requests
from datetime import datetime
import os

GENDER = "male"
WEIGHT_KG = 58.740212
HEIGHT_CM = 177.8
AGE = 13

APP_ID = os.environ["be2*****"]
API_KEY = os.environ["4fa82da*************************"]

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
sheet_endpoint = os.environ["https://api.sheety.co/0a5644021c9c3815973ccd3f25595467/myWorkouts/sheet1"]

exercise_text = input("Tell me which exercises you did: ")

headers = {
    "x-app-id": APP_ID,
    "x-app-key": API_KEY,
}

parameters = {
    "query": exercise_text,
    "gender": GENDER,
    "weight_kg": WEIGHT_KG,
    "height_cm": HEIGHT_CM,
    "age": AGE
}

response = requests.post(exercise_endpoint, json=parameters, headers=headers)
result = response.json()

today_date = datetime.now().strftime("%d/%m/%Y")
now_time = datetime.now().strftime("%X")

bearer_headers = {
    "Authorization": f"Bearer {os.environ['TOKEN']}"
}

for exercise in result["exercises"]:
    sheet_inputs = {
        "workout": {
            "date": today_date,
            "time": now_time,
            "exercise": exercise["name"].title(),
            "duration": exercise["duration_min"],
            "calories": exercise["nf_calories"]
        }
    }

    sheet_response = requests.post(sheet_endpoint, json=sheet_inputs, headers=bearer_headers)

    print(sheet_response.text)

Output:

Traceback (most recent call last):
  File "/Users/ale******fer******/PycharmProjects/test/main.py", line 10, in <module>
    APP_ID = os.environ["be25d95e"]
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'be25d95e'

Answers:

That’s saying you do not have be25d95e as an environment variable. Based on what I’m seeing here, you need to remove os.environ[] from all three places you call it. You have hardcoded the strings. You are not reading from the environment. So

APP_ID = "be25d95e"
API_KEY = "4fa82da..."
and
sheet_endpoint = "https://..."
Answered By: Tim Roberts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.