34

I'm trying to access a google app through the Python Client using this code to gain authorization (private info obviously redacted):

import gflags
import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import SignedJwtAssertionCredentials
from oauth2client.tools import run

f = open('privatekey.p12', 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(
    service_account_name='[email protected]',
    private_key=key,
    scope = 'https://www.googleapis.com/auth/calendar')
http = httplib2.Http()
http = credentials.authorize(http)
service = build(serviceName='calendar', version='v3', http=http)

Yet I receive this error:

ImportError: cannot import name SignedJwtAssertionCredentials

I have installed the Google v3 API Python Client as well as OAuth2; I don't seem to be having any other problems with those modules, though I haven't used them much. Anyone know what's going on?

2

8 Answers 8

75

I had this problem today and had to roll back from oauth2client version 2.0 to version 1.5.2 with:

pip install oauth2client==1.5.2
4
  • 2
    Thanks bro. After a very long journey your answer finally helped :-)
    – Md. Mohsin
    Commented Feb 22, 2016 at 18:57
  • 5
    As michael mentioned above github.com/google/oauth2client/issues/401 explains that SignedJwtAssertionCredentials has been removed and its behaviour is now implemented in auth2client.service_account.ServiceAccountCredentials
    – Caz
    Commented Feb 24, 2016 at 13:57
  • 3
    This fix does not work with google api client anymore, since it has been updated to require oauthclient 2.0+. Changing SignedJwtAssertionCredentials to ServiceAccountCredentials as outlined above is what you want to do. Commented Apr 26, 2016 at 9:41
  • 1
    why this comment is not the answer. I am using Baton and i face the same issue, this was the only answer that work for me Commented Apr 26, 2017 at 19:42
22

It seems like you havn't installed pyopenssl. Install via easy_install pyopenssl.

Libraries oauth2client.client
if HAS_OPENSSL:
  # PyOpenSSL is not a prerequisite for oauth2client, so if it is missing then
  # don't create the SignedJwtAssertionCredentials or the verify_id_token()
  # method.

  class SignedJwtAssertionCredentials(AssertionCredentials):
....
1
  • 11
    I have PyOpenSSL installed (sudo pip install pyopenssl) and I still received the error in question (using Python 2.7 on OSX 10.8.5). My fix was to run sudo pip install pyopenssl --upgrade. Commented Jan 27, 2014 at 21:52
9

The source repository was recently updated, to make use of the new code:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

...
4

As alexander margraf said you need PyOpenSSL to import SignedJwtAssertionCredentials

simply: pip install pyopenssl

REMEMBER: It will fail on Windows if you don't have OpenSSL Win32 libs installed http://slproweb.com/products/Win32OpenSSL.html (you need full package, not the light version). Also keep in mind you need to add it to your path var before installing pyopenssl

1
  • Without OpenSSL Win32 installing pyopenssl fails with following error: 'error: Only found improper OpenSSL directories: ...'
    – Bartoszer
    Commented Mar 27, 2013 at 9:05
3

I was trying to build a local dev environment and none of the solutions here were working. The extra piece in the puzzle for me was:

$ pip install pycrypto

possibly in addition to any or all of:

$ pip install pyopenssl
$ pip install httplib2
$ pip install oauth2client
$ pip install ssl

GAE has the pycrypto package available internally (check the libraries listed in your app.yaml) so something needing it might work on their machines but not yours - I think - sorry I'm not yet clear on what and why they're making life so miserable with the libraries yet.

2

Check your oauth2client version first.

If this version >= 2.0, using the ServiceAccountCredentials instead of SignedJwtAssertionCredentials.

Look at the three reference:

2

You can try this for oauth2client version >= 2.0,

from oauth2client.service_account import ServiceAccountCredentials

ServiceAccountCredentials.from_p12_keyfile(
    service_account_email='[email protected]', 
    filename=KEY_PATH, 
    scopes='https://www.googleapis.com/auth/calendar')
1

Check your `oauth2client' module version, probably you are using greater then 1.5.2 version if that is so, you can fix this problem by downgrading the version and reinstalling the 1.5.2 or 'oauth2client.client.AccessTokenCredentials'. Documentation link https://oauth2client.readthedocs.io/en/latest/source/oauth2client.client.html

1
  • did you read through the other answers already posted? please add detail explaining why your answer adds important information not already present in the existing answers
    – landru27
    Commented Nov 11, 2018 at 20:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.