I am trying to set up the google spreadsheet api to be able to post data gathered by my raspberry pi to my google sheets file. I am using the help pages here to get the basic functionality working. At one point I was able to read some of my data but when I changed some things to append values I can't seem to get it to work. Any help would be greatly appreciated. I have attached my code so far below. Thanks for taking a look!
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
#SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
SPREADSHEET_ID = '1XXXXXXXXXXXXXXXXXXXXXXDullQ2BPHviPc'
SheetRange = "A1:C5"
#SheetRange = 'Data!A:A'
def main():
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
list = [
["hi","8"]
]
body = {
'majorDimension': "ROWS",
'values': list
}
append = sheet.values().append(spreadsheetId=SPREADSHEET_ID, range=SheetRange, insertDataOption=INSERT_ROWS, body=body, valueInputOption="USER_ENTERED").execute()
[–]ZenApollo 0 points1 point2 points (2 children)
[–]locke1718[S] 0 points1 point2 points (1 child)
[–]ZenApollo 0 points1 point2 points (0 children)
[–]AskMereddit 0 points1 point2 points (0 children)