-
-
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 | |
) |
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
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"]