-
-
Save TheMuellenator/974c39779ec516c4c60e918c001e48ba to your computer and use it in GitHub Desktop.
#No Authentication | |
sheet_response = requests.post(sheet_endpoint, json=sheet_inputs) | |
#Basic Authentication | |
sheet_response = requests.post( | |
sheet_endpoint, | |
json=sheet_inputs, | |
auth=( | |
YOUR USERNAME, | |
YOUR PASSWORD, | |
) | |
) | |
#Bearer Token Authentication | |
bearer_headers = { | |
"Authorization": f"Bearer {YOUR TOKEN}" | |
} | |
sheet_response = requests.post( | |
sheet_endpoint, | |
json=sheet_inputs, | |
headers=bearer_headers | |
) |
Still errors coming someone help me fix it
Tell me which exercises you did: Cycled 45km { "errors": [ { "detail": "Cannot read property 'projects' of null" } ] }
pls can any one help with this problem
Still errors coming someone help me fix it
Tell me which exercises you did: Cycled 45km { "errors": [ { "detail": "Cannot read property 'projects' of null" } ] }pls can any one help with this problem
please list your code
I wrote a code mentioned below and will work..try this
USERNAME =your username that you save before
PASSWORD =your password hat you save before
authorization_header = {
'Authorization': 'Basic your provided number '
}
response = requests.post(sheety_endpoint, json=sheety_request_body,auth=(USERNAME,PASSWORD),headers=authorization_header)
this is what getting an error
{'exercises': [{'tag_id': 317, 'user_input': 'running', 'duration_min': 31.08, 'met': 9.8, 'nf_calories': 390.88, 'photo': {'highres': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/317_highres.jpg', 'thumb': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/317_thumb.jpg', 'is_user_uploaded': False}, 'compendium_code': 12050, 'name': 'running', 'description': None, 'benefits': None}]}
{'errors': [{'detail': "Bad Request. The JSON payload should be inside a root property called 'sheet1'. Check https://sheety.co/docs for more details."}]}
hi, in your endpoint your sheet has name "sheet1" not "workouts" so probably You should change name from "workouts" to "sheet1" in for loop,
"detail": "POST has been disabled on this sheet."
I Keep on getting this error message when I try POST a workout.
nipsalvin
In sheety , go to your projects and click on your spreadsheet name , scroll down
you will find something like this...
POST Add a row to your sheet (enable it.)
nipsalvin In sheety , go to your projects and click on your spreadsheet name , scroll down you will find something like this... POST Add a row to your sheet (enable it.)
I was able to sort it.
Thanks
Actually, can't get it to work with the docs guide, just this repos works for me...
DIDN'T WORK
AuthEndpoint = "https://httpbin.org/basic-auth/user/pass"
basic = HTTPBasicAuth(username=MY_USERNAME, password=MY_PASSWORD)
auth_response = requests.get(url=AuthEndpoint, auth=basic)
WORKED
sheet_response = requests.post(url=SheetEndpoint, json=params, auth=(MY_USERNAME, MY_PASSWORD))
print(sheet_response.text)
Hey guys try this. I'm sure this will work.
Before running your code, make sure to cross check the authentication method you are using.
If using the basic method make sure that you have selected the basic radio button on the Sheety website under Authentication and saved the changes.
Same for the bearer method. Then simply put the Authorization Header like this before posting the request . Here using the basic method:
basic_auth_headers = {
"Authorization": "Basic TK51cmSnODk6a25zcmhkdTc2Z2R0dzU+"
}
for exercise in result["exercises"]:
exercise_data = {
"workout": {
"date": date_today,
"time": time_now,
"exercise": exercise["name"].title(),
"duration": exercise["duration_min"],
"calories": exercise["nf_calories"],
}
}
sheet_response = requests.post(
url=sheet_endpoint,
json=exercise_data,
headers=basic_auth_headers
)
#----------------------------- CODE ---------------------------------------------------------------#
APP_ID = "api Id"
API_KEY = "api key"
SHEETY_UNAME = "username"
PROJECT_NAME = "Exercise Tracker"
SHEET_NAME = "Exercise_tracker_worksheet"
GENDER = "Male"
WEIGHT = 45
AGE = 20
HEIGHT = 171
import requests
from datetime import datetime
exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
sheet_endpoint = f"https://api.sheety.co/{SHEETY_UNAME}/{PROJECT_NAME}/{SHEET_NAME}"
exercise_text = input("Tell me which exercise you did: ")
headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY
}
exercise_params = {
"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT,
"height_cm": HEIGHT,
"age": AGE
}
response = requests.post(url=exercise_endpoint,headers=headers,json=exercise_params)
print(response.status_code)
result = response.json()
current_date = datetime.now().strftime("%d%m%Y")
current_time = datetime.now().strftime("%X")
print(current_time)
for exercise in result["exercises"]:
sheet_params = {
SHEET_NAME: {
"date": current_date,
"time": current_time,
"exercise": exercise["name"].title(),
"duration": exercise["duration_min"],
"calories": exercise["nf_calories"]
}
}
sheet_response = requests.post(url=sheet_endpoint,auth=("uname","password"),json=sheet_params)
print(sheet_response.status_code)
print(sheet_response.text)
#----------------------------- output --------------------------------------------------------------#
500
{
"errors": [
{
"detail": "Cannot read property 'projects' of null"
}
]
}
#----------------------------------------Problem---------------------------------------------------#
Anyone please help me to solve this problem🙏
My Whatsapp no :- 6387656066
Mail: dineshamrawatisingh@gmail.com
did your using pycharm before you run please check sheet data with sheety your giving data make sure check spells also
i have a problem those when i run my code i get this printed out and i have asked and was told it means the code ran successfully but when i check my sheet nothing was updated and my code has been running fine but has never been posting nothing
{
"sheet1": {
"id": 2
}
}
How to remove this error:
{
"sheet1": {
"id": 2
}
}
Well you just have to format each key (date, time, exercise, duration and calories from my example) to a lower case format according to the field names you are using in your Google Sheet.
today_date = datetime.now().strftime("%d/%m/%Y")
So, you can't give a key name of ("Date_Today" : today_date) instead you have to write "date_today" : today_date
from os import environ
import requests
import datetime
NUTRITIONIX_NATURAL_ENDPOINT = "https://trackapi.nutritionix.com/v2/natural/exercise"
NUTRITIONIX_API_ID = environ.get("NUTRITIONIX_API_ID")
NUTRITIONIX_API_KEY = environ.get("NUTRITIONIX_API_KEY")
SHEETY_WORKOUT_ENDPOINT = environ.get("SHEETY_WORKOUT_ENDPOINT")
SHEETY_WORKOUT_TOKEN = environ.get("SHEETY_WORKOUT_TOKEN")
WT_GENDER = "male"
WT_HEIGHT = "178"
WT_WEIGHT = "90"
WT_AGE = 55
def nutritionix_calculate_calories_from_query(api_id, api_key, query, gender, height, weight, age):
header = {
"x-app-id": api_id,
"x-app-key": api_key,
"Content-Type": "application/json"
}
body = {
"query": query,
"gender": gender,
"weight_kg": weight,
"height_cm": height,
"age": age,
}
response = requests.post(url=NUTRITIONIX_NATURAL_ENDPOINT, headers=header, json=body)
return response.json()["exercises"]
def sheety_get_workout():
header = {
"Authorization": f"Bearer {SHEETY_WORKOUT_TOKEN}"
}
response = requests.get(url=SHEETY_WORKOUT_ENDPOINT, headers=header)
print(response.text)
def sheety_post_workout(exercises):
now = datetime.datetime.now()
for exercise in exercises:
header = {
"Authorization": f"Bearer {SHEETY_WORKOUT_TOKEN}"
}
body = {
"workout": {
"date": now.strftime("%d/%m/%Y"),
"time": now.strftime("%X"),
"exercise": exercise["user_input"].title(),
"duration": exercise["duration_min"],
"calories": exercise["nf_calories"]
}
}
response = requests.post(url=SHEETY_WORKOUT_ENDPOINT, headers=header, json=body)
print(response.text)
def main():
query = input("Tell me wich exercice you did: ")
data = nutritionix_calculate_calories_from_query(api_id=NUTRITIONIX_API_ID,
api_key=NUTRITIONIX_API_KEY,
query=query,
gender=WT_GENDER,
height=WT_HEIGHT,
weight=WT_WEIGHT,
age=WT_AGE)
sheety_post_workout(exercises=data)
sheety_get_workout()
if name == "main":
main()
{'sheet1': {'id': 3}} i get this output but my spreadsheet didn't change?????
{'errors': [{'detail': 'The caller does not have permission'}]}
Please how do I rectify this, anyone?please share your code.
Solution: First make a copy of the sheet with a new name and then make sure that the email id used for logging to google sheets is the same in sheety as well.
This should help rectify the problem.
solucao desse erro
User
{
"errors": [
{
"detail": "The caller does not have permission"
}
]
}
Process finished with exit code 0
solucao pra mim
pra quem ta tendo esse erro simples apague seu projeto atual da API sheety cria uma nova planilha ou copia da angela click na planilha e pega link dessa planilha coloca quando for cria o projeto de novo na api sheety onde pede o link de sua planilha aqui resolveu assim,
demorou muito pra mim resolver isso ..
Can the USERNAME, PASSWORD , and TOKEN be any?
Yes.
{'sheet1': {'id': 3}} i get this output but my spreadsheet didn't change?????
id is an "extra data point" that sheety will generate. It won't be part of the column names you are trying to populate.
Ladies and gentlemen
@shoaibkhan5676 @Sir-AryanG-NTfV @MahalakahmiMallela and everyone else
for debugging :
-
Check first that you have actually created a new sheet to be able to edit it, not the one provided by angela.
-
Once you create the sheet make sure you add the headers ( date , time , exercise,duration, calories ) so that it will be able to update and avoid giving you (undefined 0 ) and other error {
"sheet1": {
"id": 2
}
} -
most importantly remember to change the ""workout stated in angela sheet """ in the for loop .
for exercise in result["exercises"]:
sheet_inputs = {
here instead of workouts place the name of your sheet, sheet1 in my example. "sheet1": {
"date": today_date,
"time": now_time,
"exercise": exercise["name"].title(),
"duration": exercise["duration_min"],
"calories": exercise["nf_calories"]
I think anyone who has the error of unauthorized can check both requests of the first post and the second get, if one of the request you forget to paste auth or token, you will get the error. PLS CHECK THE BOTH REQUESTS!
BEARER TOKEN AUTHENTICATION FIX
For me it was probably the bearer token you create at Sheety, I imagine it didn't meet some parameters so I tried copying the token that gets created for you in the Basic authentication from username and password and changed it a little and that worked.
Hope it helps someone
It works like that:
basic = HTTPBasicAuth('username', 'pass')
responses = requests.post(url=SHEETY_API, json=my_data, auth=basic)
So much easier just using the basic authentication. like the comment above just don't forget
from requests.auth import HTTPBasicAuth
- I was able to fixed the problem after removing the "Bearer" from the Authorization header. i.e
" req_body = {
"workout" : {
"date": date,
"time": current_time,
"exercise": exercise.title(),
"duration": duration,
"calories": calories
}
}
sheety_header = {
"Authorization": f"Bearer {bearer_token}"
}
sheety_url = "https://api.sheety.co/YOUR_KEY/copyOfMyWorkouts/workouts"
response = requests.post(url=sheety_url, json=req_body, headers=sheety_header)
"
**To This:**
"req_body = {
"workout" : {
"date": date,
"time": current_time,
"exercise": exercise.title(),
"duration": duration,
"calories": calories
}
}
sheety_header = {
**"Authorization": f"{bearer_token}"**
}
sheety_url = "https://api.sheety.co/YOUR_KEY/copyOfMyWorkouts/workouts"
response = requests.post(url=sheety_url, json=req_body, headers=sheety_header)"
2. And you can also check your google account under security, and make sure you allow Sheety access to your google sheet.
3. Check your endpoint well
yes, that worked for me as well.