I wrote this piece of code on my pycharm which works and still works anytime I run it on pycharm. But I needed to put the code on replit and it keeps giving me 401 client error on replit
On replit I had to put the keys in Environment variable using os modude
import requests
from requests.auth import HTTPBasicAuth
from datetime import datetime
import os
APP_ID = os.environ['APP_ID']
API_KEY = os.environ['API_KEY']
URL = "https://trackapi.nutritionix.com/v2/natural/exercise"
SHEET_ENDPOINT = "https://api.sheety.co/87b10732a09117567a70b4341498006c/workoutTracking/workouts"
QUERY = input("Tell me what exercise you did: ")
GENDER = "female"
WEIGHT_KG = 72.5
HEIGHT_CM = 167.64
AGE = 30
headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY
}
parameters = {
"query": QUERY,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE
}
response = requests.post(url=URL, json=parameters, headers=headers)
response.raise_for_status()
data = response.json()
print(data)
# step 4
exercise = data["exercises"][0]
today_date = datetime.now().strftime("%d/%m/%Y")
now_time = datetime.now().strftime("%X")
INPUT = {
"workout": {
"date": today_date,
"time": now_time,
"exercise": exercise["name"].title(),
"duration": exercise["duration_min"],
"calories": exercise["nf_calories"]
}
}
add_row = requests.post(url=SHEET_ENDPOINT, json=INPUT, auth=HTTPBasicAuth("david", "sheetypassword"))
add_row.raise_for_status()
print(add_row.text)
It gave me this error:
Traceback (most recent call last):
File "/home/runner/Exercise-tracking/main.py", line 34, in <module>
response.raise_for_status()
File "/home/runner/Exercise-tracking/.pythonlibs/lib/python3.10/site-packages/requests/models.py", line 1024, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://trackapi.nutritionix.com/v2/natural/exercise
New contributor
David Oladeji is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.