Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Forked from angelabauer/main.py
Last active May 15, 2024 14:22
Show Gist options
  • Save TheMuellenator/974c39779ec516c4c60e918c001e48ba to your computer and use it in GitHub Desktop.
Save TheMuellenator/974c39779ec516c4c60e918c001e48ba to your computer and use it in GitHub Desktop.
Day 38 Step 5 L335 - Solution
#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
)
@Mumartayyab
Copy link

Screenshot 2022-08-10 135156 I have tried all the solutions but it still shows me: "detail": "The caller does not have permission" What can I do next anyone?

Where you able to figure this out? stuck with this message also

Just Create a new spreadsheet and link it with your project.The sheet you are using is in read only mode!

@Sir-AryanG-NTfV
Copy link

Sir-AryanG-NTfV commented Sep 28, 2023

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

image

@AbdullahZulfikar
Copy link

image

I was having a similar error but I turn off the authorization module. And it worked

becasue when ur using the Authorization ur not porviding any toekn or any thing is missing u have to provide ur token if ur using Authorization

@pedsf1968
Copy link

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()

@shoaibkhan5676
Copy link

{'sheet1': {'id': 3}} i get this output but my spreadsheet didn't change?????

@kausy-123
Copy link

kausy-123 commented Dec 16, 2023

{'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.

@Edcarloszaya
Copy link

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 ..

@Siris4
Copy link

Siris4 commented Feb 2, 2024

Can the USERNAME, PASSWORD , and TOKEN be any?

Yes.

@Siris4
Copy link

Siris4 commented Feb 2, 2024

{'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.

@MohamedHelmyCG
Copy link

Ladies and gentlemen
@shoaibkhan5676 @Sir-AryanG-NTfV @MahalakahmiMallela and everyone else
for debugging :

  1. Check first that you have actually created a new sheet to be able to edit it, not the one provided by angela.

  2. 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
    }
    }

  3. 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"]

@j10251025
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment