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": "XXXXgkcvcke@developer.gserviceaccount.com",
"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()