0

I am new to python and have been working with gspread for a couple months now and keep getting it working then it comes up with an error the next day!

I have an RFID module sending data to google sheets. Sometimes Google API accepts the data, sometimes it doesn't and I really can't figure out the solution after searching for a few days now (tearing my hair out).

I am using Raspberry Pi 3, python 2.7 and gspread to send to google drive API where the info is logged in google docs. I have reset the ntp servers to local ones so the time is correct and I've even set a cron reboot to happen during the day to see if it will refresh any error that pops up. I have the .py file and .json on the pi's desktop.

This is my error: "HttpAccessTokenRefreshError: invalid_grant: Invalid JWT Signature."

Code is below. Thanks for any help or suggestions :)

import serial
import re
import time
import gspread
import json

from oauth2client.service_account import ServiceAccountCredentials

pattern = r"(\d+\.\d*)_(\d+\.\d*)\r\n"enter code here
#port = '/dev/ttyACM1'
port2 = "/dev/ttyACM0" 
scope = ['https://spreadsheets.google.com/feeds']

credentials = ServiceAccountCredentials.from_json_keyfile_name('My Project-xxxxxxxxxxxx.json', scope)
ser = serial.Serial(port2, 115200, timeout = 1)

if ser.isOpen():
    print "connected"

    while True:
        line = ser.readline()
        if len(line) > 2: 
            m = re.match(pattern, line)
            tableline = [str(time.strftime("%d/%m/%Y")), str(time.strftime("%H:%M:%S")), line]# previously m.group(1), m.group(2)
            gc = gspread.authorize(credentials)
            wks = gc.open("example").sheet1
            wks.append_row(tableline)

            print (tableline)
            time.sleep(30)
        else:
                time.sleep(1)

    else:
                    print ("Failure opening serial port at " + port)
scrawney
  • 1
  • 1
  • I think the answer I gave [here](http://stackoverflow.com/questions/38880188/gspread-w-oauth2client-service-account-key/38902952#38902952) might account for what you're seeing here. – roganjosh Mar 01 '17 at 16:20
  • Actually, I missed `gc = gspread.authorize(credentials)`. So in theory you authorise on each cycle of the loop? – roganjosh Mar 01 '17 at 16:26
  • Yeah when data is coming in from the serial port it should authorise the credentials. It normally works consistently for a few hours while testing then drops off and produces the error. – scrawney Mar 01 '17 at 16:35
  • That's a pain. However, my general approach _may_ help in that you simply sleep for 30 seconds on exception then have another go. I've had gigantic html errors coming through from google and all sorts for no good reason, but it rectifies itself. – roganjosh Mar 01 '17 at 16:37
  • Now I am getting 'AttributeError: 'module' object has no attribute 'HTTPError' - is this not an exception? – scrawney Mar 02 '17 at 11:51
  • Hmm, it seems the latest code on github does not contain that exception. I don't know for sure which of the new exceptions would handle your error, you could test with just `except Exception as e:` and run it until you your exception again and figure out its new name. – roganjosh Mar 02 '17 at 11:58
  • Looking at my source code, there's actually a deprecation note on my exception. It looks like you might use `gspread.exceptions.RequestError` instead now but again, not sure. – roganjosh Mar 02 '17 at 12:09
  • Ok will give that a go. I have put a ntp reset in the /etc/rc.local file so the time is now bang on. Hopefully this will eliminate any minor discrepancies in that department. Will let you know how it goes tomorrow after an evening on with that in place. – scrawney Mar 02 '17 at 18:01

0 Answers0