Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oauth2 credentials not working #224

Closed
elliotslab opened this issue Apr 23, 2015 · 14 comments
Closed

Oauth2 credentials not working #224

elliotslab opened this issue Apr 23, 2015 · 14 comments

Comments

@elliotslab
Copy link

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.

@ghost
Copy link

ghost commented Apr 23, 2015

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

@elliotslab
Copy link
Author

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

@ghost
Copy link

ghost commented Apr 24, 2015

Same here

@elliotslab elliotslab changed the title Oauth2 credentials not workin Oauth2 credentials not working Apr 24, 2015
@burnash
Copy link
Owner

burnash commented Apr 24, 2015

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

@elliotslab
Copy link
Author

@burnash thanks is workning great now.

I share spreadsheet with the client_email in my .json

@kuzmeech
Copy link

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

@pdgonzalez872
Copy link

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

@joyalexandre
Copy link

Thanks this helped me a lot. :-)

@frankxin
Copy link

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 ?

@joyalexandre
Copy link

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

@elliotslab
Copy link
Author

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

@felagund
Copy link

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.

@ghost
Copy link

ghost commented Jun 17, 2015

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

@jbjbro
Copy link

jbjbro commented Jul 2, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants