Forums

Refreshing access_token error using gspread

I have a simple app that provides a random quote on a website, taken from a Google spreadsheet using the Python library, gspread. There is a Javascript reload button that allows the user to refresh the page and get a new random quote. I am using OAuth2 credentials to authorize the call to my spreadsheet. Here's a snippet:

json_file = open(os.path.join(current.request.folder, 'private', 'quote_generator.json'))

json_key = json.load(json_file)

scope = ['https://spreadsheets.google.com/feeds']

credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)

gc = gspread.authorize(credentials)

Refreshing the page generates a new quote for the first few times but then errors. Looking at my error log, all of the errors are from "Refreshing access_token", which I think is an OAuth problem. How can I fix this so that users can refresh the quotes as many times as they like?

In order to get an access_token you need a refresh_token and to get you need to request 'offline access'. While no answering your question, hopefully that will get you on the right path.

Thanks, I've tried this and so far it looks like it's working: https://github.com/burnash/gspread/issues/181#issuecomment-127789385

Actually, that hasn't fixed it :-( Will look further into your suggestion.

I added a credentials.refresh as suggested here: https://github.com/burnash/gspread/issues/333

This half fixed it. I have just added in a section of code to my controller to attempt three times before redirecting to an error page and this also looks like it has helped.