Oauth2 credentials not working
Today a deprecation warning prevent me from starting my script so after I follow this instructions but after running my script got this error
File "main.py", line 12, in <module>
credentials = SignedJwtAssertionCredentials(json_key['client_email'],json_key['private_key'], scope)
File "/home/oscar/.virtualenvs/google/lib/python3.4/site-packages/oauth2client/util.py", line 137, in positional_wrapper return wrapped(*args, **kwargs)
File "/home/oscar/.virtualenvs/google/lib/python3.4/site-packages/oauth2client/client.py", line 1458, in __init__
_RequireCryptoOrDie()
File "/home/oscar/.virtualenvs/google/lib/python3.4/site-packages/oauth2client/client.py", line 1412, in _RequireCryptoOrDie
raise CryptoUnavailableError('No crypto library available')
oauth2client.client.CryptoUnavailableError: No crypto library available
here the error is clearly because there's not crypto library but then I install pyopenssl ( I've try with both disto and pip packages ) the next error appears
Traceback (most recent call last):
File "main.py", line 12, in <module>
credentials = SignedJwtAssertionCredentials(json_key['client_email'],json_key['private_key'], scope)
File "/home/oscar/.virtualenvs/google/lib/python3.4/site-packages/oauth2client/util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/oscar/.virtualenvs/google/lib/python3.4/site-packages/oauth2client/client.py", line 1469, in __init__
self.private_key = base64.b64encode(private_key)
File "/home/oscar/.virtualenvs/google/lib/python3.4/base64.py", line 62, in b64encode
encoded = binascii.b2a_base64(s)[:-1]
TypeError: 'str' does not support the buffer interface
I'm stuck in here.
I was in the same situation and here's what I did:
- Defined GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of JSON file from Google Console
vim /etc/environment
export GOOGLE_APPLICATION_CREDENTIALS="/path/to.json"
source /etc/environment
- Instead of using SignedJwtAssertionCredentials, I've used GoogleCredentials, so no need to install pycrypto, python-openssl, etc
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
credentials = credentials.create_scoped(['https://spreadsheets.google.com/feeds'])
- Now it's just use the credentials to authorize gspread:
gs = gspread.authorize(credentials)
sheet = gs.open('file').worksheet('sheet')
References: https://developers.google.com/identity/protocols/application-default-credentials#howtheywork http://google.github.io/oauth2client/source/oauth2client.html#oauth2client.client.GoogleCredentials
This solve the authentication issue but now I can't access any of my spreadsheets by any means
This is the error
Traceback (most recent call last):
File "main.py", line 10, in <module>
wks = gs.open("Spreads").worksheet('Trigger')
File "/home/oscar/.virtualenvs/google/lib/python3.4/site-packages/gspread/client.py", line 150, in open
raise SpreadsheetNotFound
gspread.exceptions.SpreadsheetNotFound
thanks btw with the authentication issue
EDIT:
I've tried with
open('sheet_name')
open_by_key('sheet_key')
open_by_url('sheet_url')
but still doen't work
Same here
@oinirio try to share this spreadsheet with an email address listed in your credentials.
@burnash thanks is workning great now.
I share spreadsheet with the client_email in my .json
yes, sharing the spreadsheet with the cryptic "client_email" value from the json file (not just the e-mail used to create the oauth2) fixed SpreadsheetNotFound for me, which popped today morning on my spreadsheet scanner tool
And my app is using
from oauth2client.client import SignedJwtAssertionCredentials
to add to @mdalpi(Thank you by the way with the authentication issue):
This is how you can set an environment variable on Windows (my issue) with a python script:
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "path_to_file_on_pc"
Thanks this helped me a lot. :-)
Hi @oinirio , I just met the same problem as yours .
Traceback (most recent call last):
File "main.py", line 10, in <module>
wks = gs.open("Spreads").worksheet('Trigger')
File "/home/oscar/.virtualenvs/google/lib/python3.4/site-packages/gspread/client.py", line 150, in open
raise SpreadsheetNotFound
gspread.exceptions.SpreadsheetNotFound
And then , I share the sheet with client_email in json file , and it didn't work either. Will it have a long delay after i share the spreadsheet ?
It should work as soon as you share it (or the next minutes).
@frankxin just like @joyalexandre said it should be instant after you share it
I came across this when the bug in my code was an improper spelled worksheet, a good way to check what is going on is to use gs.openall() method, that lists all available spreadsheets.
Hi all,
I shared the client_email which is in my JSON. My code runs fine, with no errors. But unable to open the spreadsheet. I am struck. My issue #256
@mdalpi Thanks! Using GOOGLE_APPLICATION_CREDENTIALS Works on openwrt-yun. For those trying to use gspread on the Yun, it seems the pyopenssl version is too old (version 0.10 on Yun) and apparently oauth2client calls the method sign from pyopenssl which is undefined so using SignedJwtAssertionCredentials breaks.
For the environment variable, you must add the following line to the configuration file /etc/profile then restart: export GOOGLE_APPLICATION_CREDENTIALS="/path/to.json"