Python - problems accessing a Google spreadsheet using an OAuth 2.0 service client

797 views Asked by At

I own a Google spreadsheet which I am trying to access via a little OAuth 2.0 Python service client I am writing, using gspread and oauth2client.

I've created an OAuth 2.0 service account on Google Developers Console and have shared the spreadsheet with that email, giving the client/application access to it, and have put the JSON key file containing the credentials inside a little test script. Although I am able to construct the OAuth 2.0 credentials object and even get a spreadsheet client, on calling openall() on the client there is an XML parsing error:

File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/gspread/client.py", line 214, in openall
  feed = self.get_spreadsheets_feed()
File "/Library/Python/2.7/site-packages/gspread/client.py", line 230, in get_spreadsheets_feed
  return ElementTree.fromstring(r.read())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1301, in XML
  return parser.close()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1654, in close
  self._raiseerror(v)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1506, in _raiseerror
  raise err
xml.etree.ElementTree.ParseError: no element found: line 1, column 0

Here is the code:

import gspread
from gspread import Client
from gspread.httpsession import HTTPSession
from oauth2client.client import SignedJwtAssertionCredentials

OAuth2_JSON_key = {
  "client_auth_scope": "https://spreadsheets.google.com/feeds",
  "myspreadsheet_keys":
  {
    "myspreadsheet_key": "XXXX"
  },
  "private_key_id": "XXXX",
  "private_key": "XXXX",
  "client_email": "[email protected]",
  "client_id": "XXXXgkcvcke.apps.googleusercontent.com",
  "client_type": "service_account"
}

OAuth2_credentials = SignedJwtAssertionCredentials(
    OAuth2_JSON_key['client_email'],
    OAuth2_JSON_key['private_key'],
    OAuth2_JSON_key['client_auth_scope']
)

persistent_session = HTTPSession(headers={'Connection':'Keep-Alive'})

spreadsheet_client = gspread.Client(
    auth=OAuth2_credentials,
    http_session=persistent_session
)

spreadsheets = spreadsheet_client.openall()
2

There are 2 answers

0
AudioBubble On BEST ANSWER

From what I can see using insert_row with the gpsread.Client() method causes problems. I switched the code back to gspread.authorize() and it works perfectly fine.

1
Ilya On

I am not too familiar with this, but I was having a similar error. You should try downloading the json key from your google developer console and running this:

json_key = json.load(open('****.json'))  #Downloaded from google
credentials = SignedJwtAssertionCredentials(json_key['client_email'],   json_key['private_key'], scope)
gc = gspread.authorize(credentials)

I get no errors accessing my spreadsheets this way. Don't forget to share your spreadsheets with your client email.