-2

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
Jake Wagner
  • 786
  • 2
  • 12
  • 29
  • 1
    the `\` at the end of the line just skip the linebreak. Are you sure you want to do that? What is the data you want to put in the string (multi-line)? can you [edit] your question to provide it? – Jean-François Fabre Oct 25 '16 at 16:28
  • The private key is too long so for the sake of simplicity, I want to spread it through multiple lines that's why I have used the triple quotes. – Jake Wagner Oct 25 '16 at 16:30
  • Pythoner, I deleted my answer because looking at your example above more detailed it seems less correct. Are you intentionally breaking lines at \ with n as the first character on the next line or should that be \n together? \ at the end of the line catches the line break and then n becomes part of the string. if they were meant to be together \n then it introduces a line break, so they do very opposite things – Lost Oct 25 '16 at 16:42
  • Yes I am intentionally breaking up the lines. – Jake Wagner Oct 25 '16 at 16:45
  • If you are intentionally trying to break lines, you need to make sure the \n stay together. If you physically break them apart (as you did above) you are escaping that physical break – Lost Oct 25 '16 at 16:47
  • I have edited my original post. It still gives error @ Lost. – Jake Wagner Oct 25 '16 at 16:52
  • can you provide a [mcve] ? when I run your code I get `FileNotFoundError: [Errno 2] No such file or directory: 'test@developer.gserviceaccount.com'` – Jean-François Fabre Oct 25 '16 at 16:58
  • @ Jean afraid since it is confidential details I can't share publicly. My apologies and I appreciate your help. – Jake Wagner Oct 25 '16 at 17:01
  • I find Jean-Francois point interesting though, an e-mail address is NOT a file or directory which is what, apparently, your function is looking for in that argument position. If you look at the examples [here](https://developers.google.com/api-client-library/python/auth/service-accounts) it looks like you should be doing `from_json_keyfile_name(json_key, scope)` this will only work if the json_key file is in the current working directory though – Lost Oct 25 '16 at 17:10
  • Also, can you post your actual error along with the stack trace? – Lost Oct 25 '16 at 17:11
  • Thanks Lost, I have done the research on what to have instead of that json key and what I found was that a client email and private key should be there. Since I am still new to Python and programming general, the terminology does get baffling at times. The error reads 'EOL while scanning string liberal'. – Jake Wagner Oct 25 '16 at 19:53
  • For more details you can have a look here http://gspread.readthedocs.io/en/latest/oauth2.html. – Jake Wagner Oct 25 '16 at 20:09
  • Also please never use that private key anywhere after this post. That test key is now completely insecure (and associated with the account posted here). – Pyrce Oct 25 '16 at 20:22
  • @ Pyrce I never gave the full private key it was a part of it. Appreciate you looking out Thanks. – Jake Wagner Oct 25 '16 at 20:30
  • You should remove the PrivateKey from the code. – Spown1962 Nov 28 '16 at 13:17

1 Answers1

0

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.