ASH
ASH

Reputation: 20362

Spreadsheet Not Found Error - Trying to Access Google Sheets

I am testing this code.

import gspread
from oauth2client.service_account import ServiceAccountCredentials

# use creds to create a client to interact with the Google Drive API

scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.readonly']

creds = ServiceAccountCredentials.from_json_keyfile_name('C:\\my_path\\client_secret.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Test_Sheet").sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

I am getting this error:

SpreadsheetNotFound

I followed the instructions from the link below.

https://medium.com/datadriveninvestor/use-google-sheets-as-your-database-using-python-77d40009860f

Perhaps I missed a step in the link, but I don't think so. Maybe something changed in GCP since that article was published, a little over 1 year ago. This is likely, as the cloud is a very dynamic place. What is the best way to trouble shoot this kind of issue? I would really love to get this thing working, mostly as a learning exercise, but I can also see this being very useful, quite soon, in my line of work. Thanks.

Upvotes: 1

Views: 5260

Answers (1)

ASH
ASH

Reputation: 20362

Ok, I finally figured it out. Face plant here...

In the file named 'statup_funding.json', you have to grab the email that is generated, then click the 'Share' button on the Spreadsheet, and paste THAT EMAIL ADDRESS into the window that opens. Then, everything works as expected. Here is a final version of my now-working code.

import gspread
#Service client credential from oauth2client
from oauth2client.service_account import ServiceAccountCredentials
# Print nicely
import pprint
#Create scope
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
#create some credential using that scope and content of startup_funding.json
creds = ServiceAccountCredentials.from_json_keyfile_name('startup_funding.json',scope)
#create gspread authorize using that credential
client = gspread.authorize(creds)
#Now will can access our google sheets we call client.open on StartupName
sheet = client.open('Test_Sheet').sheet1
pp = pprint.PrettyPrinter()
results = sheet.get_all_records()
results

Upvotes: 1

Related Questions