Search code examples
pythonpython-3.xoauth-2.0google-api-python-clientpyopenssl

OpenSSL.crypto.Error: [] in SignedJwtAssertionCredentials attempt


I'm trying to login with SignedJwtAssertionCredentials to get gspread to work. I've followed the directions here with the notes for python3. Here's my class:

import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials

    class GoogleSpreadsheet():
        def fetch(self):
            gc = gspread.authorize(self.credentials)
            wks = gc.open("TEST of Sheet (Responses)").sheet1
            print(wks)

        def __init__(self, client_email,pk):
                scope = ['https://spreadsheets.google.com/feeds']
                self.credentials = SignedJwtAssertionCredentials(client_email, bytes(pk, 'utf-8'), scope)

with client_email and pk being passed from environment variables:

gs = GoogleSpreadsheet(app.config.get('GOOGLE_SPREADSHEET_CLIENT_EMAIL'), app.config.get('GOOGLE_SPREADSHEET_PK'))

The environment variable is copied from the google auth json file:

export GOOGLE_SPREADSHEET_PK="-----BEGIN PRIVATE KEY-----\n...\n...keystuff...\n...\n-----END PRIVATE KEY-----\n"

It errors out every attempt with OpenSSL.crypto.Error: []

The suggestion here Loading private key fails with OpenSSL.crypto.Error: [] didn't help since my key isn't encrypted.

Any suggestions?


Solution

  • With some help from this thread: https://github.com/google/oauth2client/issues/193 and some fiddling, it looks like you need to turn the \n's in the private key into real newlines if you're not loading the config file though JSON.load (which the gsheets tutorial does.)