-
-
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 | |
| ) |
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
the next wored for me too:
headers_sheety = { "Authorization": MY AUTHORIZATION HEADER, }
sheety_response = requests.post(url=ENDPOINT, json=new_workout_data, headers=headers_sheety) print(sheety_response.text)
yes, that worked for me as well.
Yup, i used this code instead of my password...and i worked
For some reason, the info doesn't get populated into google sheets. I have checked that the permissions are given to Sheety on my gmail account, and the data is nested in the API root.
import requests
from datetime import datetime
API_KEY = "nix_live_igDg82h4BmwMowKrwnL0TAzILSNKwXzG"
API_ID = "app_eef0ff9273e147b6859e10db"
endpoint = "https://app.100daysofpython.dev/v1/nutrition/natural/exercise"
sheety_token = "Bearer 48fnef@49%)#)@Ejdnce03r38u3rf"
sheety_endpoint = "https://api.sheety.co/121544da6b8eef4be1a71d43d91442d3/ramaWorkouts/workouts"
headers = {
"Content-Type": "application/json",
"x-app-id": API_ID,
"x-app-key": API_KEY
}
workout_data = {
"query":"ran 24km",
"weight_kg":72.3,
"height_cm":171,
"age": 33,
"gender":"male"
}
workout_info = requests.post(url = endpoint, json = workout_data, headers= headers)
result = workout_info.json()
headers = {
"Authorization":sheety_token
}
workout_date = datetime.today().strftime("%d/%m/%Y")
workout_time = datetime.now().strftime("%H:%M:%S")
for exercise in result['exercises']:
workout_input_data = {
"workout": {
"Date":workout_date,
"Time": workout_time,
"Exercise":exercise['name'].title(),
"Duration":exercise['duration_min'],
"Calories":exercise['nf_calories']
}
}
workout_data_to_sheets = requests.post(url = sheety_endpoint, json = workout_input_data)
workout_data_to_sheets.raise_for_status()
print(workout_data_to_sheets)
{'sheet1': {'id': 3}} i get this output but my spreadsheet didn't change?????
for exercise in result["exercises"]:
sheet_inputs = {
"sheet1": {

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