1

I am attempting to connect to this Google Sheet: https://docs.google.com/spreadsheets/d/1u3Ql_Rs19PEe0mwqpsiw2wf2cUEbnSG4vZL63rh_OaE/edit?usp=sharing

Both the .json file and the .py file are saved to my desktop.

The email in the .json has been shared with the Google Sheet

I am getting a 'JSONDecodeError: Expecting value' and cannot figure out why.

I have revised the client id and key info in the .json for pirvacy as I am new to this and was not sure if sharing this was wise

Any help is greatly appreciated. thank you

python:

import gspread

gc = gspread.service_account(filename = 'credentials.json')

sh = gc.open('User')

worksheet=sh.sheet1

res=worksheet.get_all_records()

print(res)

credentials.json:

{
  "type": "service_account",
  "project_id": "clear-heaven-331616",
  "private_key_id": "abcxyz",
  "private_key": "-----BEGIN PRIVATE KEY-----\abcxyz=\n-----END PRIVATE KEY-----\n",
  "client_email": "[email protected]",
  "client_id": "abcxyz",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/my-service-account%40clear-heaven-331616.iam.gserviceaccount.com"
}

traceback:

  File "C:\Users\dusti\Desktop\googlesheets.py", line 10, in <module>
    gc = gspread.service_account(filename = 'credentials.json')

  File "C:\Users\dusti\anaconda3\lib\site-packages\gspread\auth.py", line 196, in service_account
    creds = ServiceAccountCredentials.from_service_account_file(

  File "C:\Users\dusti\anaconda3\lib\site-packages\google\oauth2\service_account.py", line 238, in from_service_account_file
    info, signer = _service_account_info.from_filename(

  File "C:\Users\dusti\anaconda3\lib\site-packages\google\auth\_service_account_info.py", line 73, in from_filename
    data = json.load(json_file)

  File "C:\Users\dusti\anaconda3\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),

  File "C:\Users\dusti\anaconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)

  File "C:\Users\dusti\anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

  File "C:\Users\dusti\anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value
2
  • what line are you getting the error on? paste in the full stacktrace. Also, have you tried using open_by_key instead? Commented Nov 9, 2021 at 21:28
  • I did try the open_by_key and had the same result. I have updated the post to show the traceback (I believe this is what you were after? new to this...). Commented Nov 9, 2021 at 22:06

1 Answer 1

1

First off I would double check that you created service account credentials on google cloud console.

Then I would consider using the Google-api-Python-client library this is googles official client library.

Then you should consider using from_json_keyfile_name

"""Hello Sheets API V4."""

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


SCOPES = ['https://www.googleapis.com/auth/drive']
KEY_FILE_LOCATION = '<REPLACE_WITH_JSON_FILE>'
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'


def initialize_sheets():
  """Initializes an sheets API V4 service object.

  Returns:
    An authorized sheets API V4 service object.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

  # Build the service object.
  sheets = build('sheets', 'v4', credentials=credentials)

  return sheets
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.