gspread icon indicating copy to clipboard operation
gspread copied to clipboard

Oauth2 credentials not working

Open elliotslab opened this issue 11 years ago • 14 comments

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.

elliotslab avatar Apr 23 '15 13:04 elliotslab

I was in the same situation and here's what I did:

  1. 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
  1. 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'])
  1. 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

ghost avatar Apr 23 '15 15:04 ghost

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

elliotslab avatar Apr 24 '15 15:04 elliotslab

Same here

ghost avatar Apr 24 '15 17:04 ghost

@oinirio try to share this spreadsheet with an email address listed in your credentials.

burnash avatar Apr 24 '15 23:04 burnash

@burnash thanks is workning great now.

I share spreadsheet with the client_email in my .json

elliotslab avatar Apr 27 '15 14:04 elliotslab

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

kuzmeech avatar Apr 28 '15 06:04 kuzmeech

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"

As seen here

pdgonzalez872 avatar May 07 '15 17:05 pdgonzalez872

Thanks this helped me a lot. :-)

joyalexandre avatar May 26 '15 22:05 joyalexandre

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 ?

Lemon7Frank avatar May 29 '15 07:05 Lemon7Frank

It should work as soon as you share it (or the next minutes).

joyalexandre avatar May 29 '15 12:05 joyalexandre

@frankxin just like @joyalexandre said it should be instant after you share it

elliotslab avatar May 29 '15 12:05 elliotslab

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.

felagund avatar May 30 '15 10:05 felagund

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

ghost avatar Jun 17 '15 14:06 ghost

@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"

jbjbro avatar Jul 02 '15 13:07 jbjbro