Gspread to access google spreadsheet: HttpAccessTokenRefreshError, invalid JWT
Asked Answered
S

1

3

I'm struggling to get access to a google spreadsheet with python 2.7 using gspread.

Here's what I have so far:

import gspread
from oauth2client.service_account import ServiceAccountCredentials



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

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'credentials.json', scope)

gc = gspread.authorize(credentials)

wks = gc.open("PracticeOpsBS").sheet1

The result is a bunch of stuff that I think is showing me what is going on, resulting in the following:

"HttpAccessTokenRefreshError: invalid_grant: Invalid JWT Signature."

Ultimately I'm hoping to access the information from two tabs of this worksheet to do some analysis, but haven't been able to pull the data in to python from google. Any help is appreciated and I'll answer any clarifying questions as I can!

Smilacaceous answered 8/6, 2016 at 18:31 Comment(5)
Is your computer indicating the right time ? https://mcmap.net/q/279107/-token-must-be-a-short-lived-token-and-in-a-reasonable-timeframeScotopia
@JacquesGaudin Computer time lines up with perfectly with NIST timeSmilacaceous
I presume that you have already tried with a different json file. Can you add the exact traceback and check your credentials.json for tokenexpired field ?Scotopia
@JacquesGaudin, I'm new to all this. Tried a different .json with no luck. Ended up downgrading oauth2client to version 1.5.1 and was able to get in. Thanks for the help!Smilacaceous
Good that you solved it I was a bit clueless on this one.Scotopia
S
1

Looks like it was an issue with the version of oauth2client that I was using. To get things to work, I downgraded to oauth2client version 1.5.1. Then I used the following to gain access to the spreadsheet:

import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials

json_key = json.load(open('credentials.json'))
scope = ['https://spreadsheets.google.com/feeds']

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


gc = gspread.authorize(credentials)

wks = gc.open('PracticeOpsBS')
Smilacaceous answered 10/6, 2016 at 20:19 Comment(1)
While this fix may work, SignedJwtAssertionCredentials has been deprecated. It is recommended that you migrate to ServiceAccountCredentials: github.com/google/oauth2client/issues/401Tater

© 2022 - 2024 — McMap. All rights reserved.