Python 导入错误:无法导入名称 SignedJwtAssertionCredentials

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14063124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:21:10  来源:igfitidea点击:

ImportError: cannot import name SignedJwtAssertionCredentials

pythongoogle-apioauth-2.0

提问by user1427661

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

我正在尝试使用此代码通过 Python 客户端访问 google 应用程序以获得授权(显然已编辑私人信息):

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?

我已经安装了 Google v3 API Python Client 和 OAuth2;这些模块似乎没有任何其他问题,尽管我没有经常使用它们。有谁知道这是怎么回事?

采纳答案by alexander margraf

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

看来您还没有安装 pyopenssl。通过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):
....

回答by Bartoszer

As alexander margraf said you need PyOpenSSL to import SignedJwtAssertionCredentials

正如 alexander margraf 所说,你需要 PyOpenSSL 来导入 SignedJwtAssertionCredentials

simply: pip install pyopenssl

简单地说: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

记住:如果您没有安装 OpenSSL Win32 库http://slproweb.com/products/Win32OpenSSL.html(您需要完整包,而不是轻量版),它将在 Windows 上失败。还要记住,在安装 pyopenssl 之前,您需要将其添加到路径 var 中

回答by John Mee

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 pycryptopackage 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.

GAE 有pycrypto内部可用的包(检查你的 app.yaml 中列出的库)所以需要它的东西可能会在他们的机器上运行,但不能在你的机器上运行 - 我想 - 抱歉,我还不清楚他们是什么以及为什么这样做对图书馆很痛苦。

回答by Locane

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

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

pip install oauth2client==1.5.2

回答by Matt

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

源代码库最近进行了更新,以使用新的代码:

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

...

回答by sih4sing5hog5

Check your oauth2clientversion first.

oauth2client首先检查您的版本。

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

如果此版本 >= 2.0,则使用ServiceAccountCredentials代替SignedJwtAssertionCredentials.

Look at the three reference:

看三个参考:

回答by Bhupinder Yadav

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

检查您的“oauth2client”模块版本,如果是这样,您可能使用的是高于 1.5.2 的版本,您可以通过降级版本并重新安装 1.5.2 或“oauth2client.client.AccessTokenCredentials”来解决此问题。文档链接https://oauth2client.readthedocs.io/en/latest/source/oauth2client.client.html

回答by E. Zeytinci

You can try this for oauth2client version >= 2.0,

你可以在 oauth2client 版本 >= 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')