Search code examples
pythongspread

FileNotFoundError when using gspread


I am trying to make a program that can add user inputs into a google sheet so I can document it. I have had smooth sailing up until now. I am following This tutorial. Now when I run my code it outputs the error:

FileNotFoundError: [Errno 2] No such file or directory: 'creds.json'

Here is the code I am running

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/sprea...',"https://www.googleapis.com/auth/drive...","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)

The file it titled "creds.json" and I currently have it sitting on my desktop (Could this be the issue?)

Thank-you for your help!


Solution

  • If you use gspread and authenticate with a service account you can simply follow the documentation

    The function service_account takes the file path of the service account JSON file and returns the client ready to use.

    Like so:

    
    gc = gspread.service_account("./my_ceeds.json")
    
    sh = gc.open("Example spreadsheet")
    
    print(sh.sheet1.get('A1'))