1
votes

I just started using Gspread and am trying to access one of my google docs spreadsheets in my google drive. I followed the instructions and went to Google API console and created a JSON file. When I run this code I get no errors:

import gspread
import json
from oauth2client.client import SignedJwtAssertionCredentials

json_key = json.load(open("mwsSearch-b3d5d5d9c956.json"))
scope = ["https://spreadsheets.google.com/feeds"]
credentials = SignedJwtAssertionCredentials(json_key['client_email'],  bytes(json_key['private_key'], 'utf-8'), scope=scope)
gc = gspread.authorize(credentials)

My next step is to try to open a google spreadsheet. I created a spreadsheet titled "Mike" in the main folder of my Google Drive, but have tried to access it via:

gc.open_by_url("https://docs.google.com/spreadsheets/d/1HH7BKsnB2Rd5rlAr7S2H3avUtO4GkqeWrXJfYKKooNA/edit#gid=0")
gc.open("Mike")
gc.open_by_key("1HH7BKsnB2Rd5rlAr7S2H3avUtO4GkqeWrXJfYKKooNA")

All three of these return the same error:

gspread.exceptions.SpreadsheetNotFound

I am thinking that maybe the api access is linking to another cloud storage through the project, and not my individual google drive, and that is why it is not accessing it. Could someone with more experience in this please point me in the right direction on what I am doing wrong. All help is appreciated. Thank you.

3

3 Answers

4
votes

You'll need to add the email which was created with the JSON key to the spreadsheet you want to access. It will be something like [email protected]. You'll find it as the "client email" in your JSON file and your credential page.

1
votes

Look at http://gspread.readthedocs.org/en/latest/oauth2.html

Go to Google Sheets and share your spreadsheet with an email you have in your json_key['client_email']. Otherwise you’ll get a SpreadsheetNotFound exception when trying to open it.