Gspread to access google spreadsheet: HttpAccessTo

2019-02-25 01:10发布

问题:

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!

回答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')