简体   繁体   中英

ImportError: cannot import name SignedJwtAssertionCredentials

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='name@developer.gserviceaccount.com',
    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?

我今天遇到了这个问题,不得不从 oauth2client 2.0 版回滚到 1.5.2 版:

pip install oauth2client==1.5.2

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):
....

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

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

...

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

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.

Check your oauth2client version first.

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

Look at the three reference:

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

You can try this for oauth2client version >= 2.0,

from oauth2client.service_account import ServiceAccountCredentials

ServiceAccountCredentials.from_p12_keyfile(
    service_account_email='name@developer.gserviceaccount.com', 
    filename=KEY_PATH, 
    scopes='https://www.googleapis.com/auth/calendar')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM