-
Notifications
You must be signed in to change notification settings - Fork 431
oauth2client
key error when instantiating a gspread
client
#207
Comments
Looks like it's related to the How are you loading Can you execute this for me: import oauth2client
print oauth2client.__version__ |
Hi, the strange thing was that it was working as written originally, and the problem only started occurring today (30/06). (Since you've replied I've replaced The The old code still causes the same RSA key error. Here, the private key, along with the other authentication credentials and the spreadsheet key were obtained from one of the custom objects on our stack called
|
That's pretty strange. How do you load the JSON contents into brand? Do you open the file in This may be related to #192, i.e. there may have been a malformed key. |
The JSON data are stored on a config web page, which is part of our stack, and this is accessible to the |
How did it get loaded into the Django form field? From the RSA error (I assume you are on [release 2.6.1], which is latest on PyPI), it seems that the value you are using doesn't have the correct newlines / whitespace. Check out the failing section (in the 2.6.1 release): |
Now I get a different error when I use the Traceback (most recent call last):
File "<console>", line 6, in <module>
File "/usr/local/lib/python2.6/dist-packages/oauth2client/service_account.py", line 51, in __init__
self._private_key = _get_private_key(private_key_pkcs8_text)
File "/usr/local/lib/python2.6/dist-packages/oauth2client/service_account.py", line 135, in _get_private_key
der = rsa.pem.load_pem(private_key_pkcs8_text, 'PRIVATE KEY')
File "/usr/local/lib/python2.6/dist-packages/rsa/pem.py", line 85, in load_pem
raise ValueError('No PEM start marker "%s" found' % pem_start)
ValueError: No PEM start marker "-----BEGIN PRIVATE KEY-----" found |
This is essentially the same as File "/usr/local/lib/python2.6/dist-packages/oauth2client/crypt.py", line 300, in from_string
pkey = RSA.importKey(parsed_pem_key)
File "/usr/local/lib/python2.6/dist-packages/Crypto/PublicKey/RSA.py", line 638, in importKey
if lines[1].startswith(b('Proc-Type:4,ENCRYPTED')):
IndexError: list index out of range which happens in I'm closing out because this confirms that you aren't loading the key correctly. Recommendations
from oauth2client.client import SignedJwtAssertionCredentials
credentials = SignedJwtAssertionCredentials(
service_account_name=client_email,
private_key=open(pkcs12_key_file_path, 'rb').read(),
scope=scope) and that you are using a JSON key with from oauth2client.client import _get_application_default_credential_from_file
credentials = _get_application_default_credential_from_file(
json_key_file_path) I'm happy to re-open if you're still having issues. |
Why does it complain that it cannot find the PEM marker when the private key does contain the marker? This is how the key starts:
|
It looks like you're loading the key incorrectly into Notice that |
I am not using
You can see from the source code http://google.github.io/oauth2client/_modules/oauth2client/client.html#GoogleCredentials that |
I don't know how those double slashes got there. |
Yes https://github.com/google/oauth2client/blob/c47dcd7f66cb8463e5432ad1f2a2a54cc4653df4/oauth2client/client.py#L1353-L1358 is the relevant section of code and you are totally right. This experience actually makes me think we need to
|
In my Sublime Text 3 I found and replaced every occurence of '\n' in the private key with an empty string, and it still complains:
|
It would be helpful if the library itself was able to remove these newline characters. The JSON key I downloaded was "straight from the source" so to speak, from Google itself. I just copied and pasted the key into our config web page, which is then accessible to our Django objects. |
The copy-paste is the point of failure.
You don't want an empty string, you want a newline character. Google itself does remove the newline charactewrs, the issue is that the literal Do this to see the difference in Python >>> a = '\n'
>>> a
'\n'
>>> print a
>>> b = '\\n'
>>> b
'\\n'
>>> print b
\n
>>> c = r'\n'
>>> c
'\\n'
>>> print c
\n |
I did this manually, i.e. replaced every occurence in the key of a '\n' character with a carriage return, and the call I didn't think that OAuth 2.0 would be this complicated! :) |
Sorry for the long road here. In the future, load the contents from the file you download instead of copying and pasting and you won't have these complications. |
In my Google Developers Console account I have created OAuth 2.0 credentials for a Google spreadsheets client I am writing using the
gspread
library - I create the client usinggspread.authorize(OAuth_credentials)
whereis the OAuth 2.0 credentials I created. But I get the following error which is related to the
oauth2client
library:The text was updated successfully, but these errors were encountered: