'Don't Work gspreads when i try to edit google sheet

I'am have a problem with gspreads, i registered API for google sheets and include json to my code, i already open access to the file for robot email, but i also see one error: raise SpreadsheetNotFound gspread.exceptions.SpreadsheetNotFound. What to do?

my code:

import gspread

sa = gspread.service_account("creds.json")
sh = sa.open("filename")

wks = sh.worksheet("Sheet1")

print(wks.row_count)

Error:

Traceback (most recent call last):
  File "C:\Users\Руслан\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gspread\client.py", line 121, in open
    self.list_spreadsheet_files(title, folder_id),
  File "C:\Users\Руслан\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gspread\utils.py", line 88, in finditem
    return next(item for item in seq if func(item))
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Руслан\Downloads\google-sheets-video-1-sacc-reading\main.py", line 4, in <module>
    sh = sa.open("url")
  File "C:\Users\Руслан\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gspread\client.py", line 129, in open
    raise SpreadsheetNotFound
gspread.exceptions.SpreadsheetNotFound

Edit: I fixed this error, but now there is another one New code:

import gspread

sa = gspread.service_account("creds.json")
sh = sa.open("filename", "D")

wks = sh.worksheet("Sheet1")

print(wks.row_count)

New error:

Traceback (most recent call last):
  File "C:\Users\Руслан\Downloads\google-sheets-video-1-sacc-reading\main.py", line 4, in <module>
    sh = sa.open('url', 'D')
  File "C:\Users\Руслан\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gspread\client.py", line 121, in open
    self.list_spreadsheet_files(title, folder_id),
  File "C:\Users\Руслан\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gspread\client.py", line 96, in list_spreadsheet_files
    res = self.request("get", url, params=params).json()
  File "C:\Users\Руслан\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gspread\client.py", line 71, in request
    raise APIError(response)
gspread.exceptions.APIError: {'errors': [{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: .', 'locationType': 'parameter', 'location': 'fileId'}], 'code': 404, 'message': 'File not found: .'}


Solution 1:[1]

From your script and SpreadsheetNotFound gspread.exceptions.SpreadsheetNotFound., I thought that in this case, the reason for your issue might be due to that the owner of the Spreadsheet of filename is not the service account or the Spreadsheet is not shared with the service account.

If your Spreadsheet of filename is in your Google Drive and the owner of it is you, how about the following methods?

  1. Share the Spreadsheet with the email of the service account.

  2. Delegating domain-wide authority to the service account

    • In this case, your account is required to be a Google Workspace account.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tanaike