Reputation: 4050
I am trying to use Gspread in Python2.7 to retrieve spreadsheets. I seem to be able to login but whenever I issue the command
gc.openall()
it just returns an empty list. I have given the service account admin access to everything and the sheets api is enabled in my google console. Can anyone point out what I am doing wrong?
Below is my name-hash.json
file returned by Google and used to login:
{
"type": "service_account",
"project_id": "name-id",
"private_key_id": "PRIVATE KEY ID",
"private_key": "-----BEGIN PRIVATE KEY-----
...
\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "CLIENTID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/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/test-279%40name-id.iam.gserviceaccount.com"
}
My code is as follows:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
def login(json_key):
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key, scope)
gc = gspread.authorize(credentials)
return gc
gc = login('/Users/username/Downloads/name-hash.json')
files = gc.openall()
print(files)
[]
Upvotes: 1
Views: 1652
Reputation: 1476
I can think of mainly 2 issues that could be at play here. First gspread may only access google sheets that are at the root of the google drive. To circumvent that, I strongly recommend moving to v4 google api python client instead of gspread.
If you do have google sheets in the root my guess is that you are missing sharing the spreadsheets themselves with the address: [email protected]
To verify that, I would suggest replacing openall with open_by_key and supply a single specific google sheet id to help debugging.
Upvotes: 1