Pythoner Pythoner - 1 month ago 20
Python Question

EOL while scanning string liberal

I am relatively new to Python so please pardon my stupidity. I want to examine google sheet data and keep running into the EOL while scanning error. I have looked at other posts and tried various tactics but none seems to be fruition. I think Client email or the private id seems to be the problem with the quotes.

import gspread
from oauth2client.service_account import *

json_key = 'gspread-test.json'
scope = ['https://spreadsheets.google.com/feeds']

credentials = ServiceAccountCredentials.from_json_keyfile_name('test@developer.gserviceaccount.com', """
-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDVS4fuXzpnKrAI\nfaZEhkNdkcHKwcbQdYOhsxVwiaMkTffExvix+Uch37JaTIyCCw4D0CKv5bR\n5FOQPAVJgciJTUKK03UZJZfgtNWVY73dBq5DTL0afI2tn9+sKgtm/BulgA\nPrKtDmlp5YX7atgXbwJTjWpZ8OLOdBtwAcL0zBYS2PR/+qNOPT1NP1tJgTEMHmbN\nuyEl0Xqqrm87Ku7eaMEcmlQrhGLH2WmpR0YEXs2hQLGx\ne/RbqrD3qr/XYbRm9TwZkCyt\n-----END PRIVATE KEY-----\n
""", scope)

gc = gspread.authorize(credentials)

wks = gc.open('Simple data').sheet1

Answer

The function ServiceAccountCredentials.from_json_keyfile_name() takes 2 parameters:

  1. A path to a .json file which contains your client ID and client secret
  2. The scope url (which you have correct)

Something like this should work:

path_to_json = <<absolute path to json file>>
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(path_to_json, scope)
client = gspread.authorize(credentials)

Then client can be used to access whatever sheets you want.