【问题标题】:ImportError: cannot import name SignedJwtAssertionCredentialsImportError:无法导入名称 SignedJwtAssertionCredentials
【发布时间】:2012-12-13 08:12:20
【问题描述】:

我正在尝试使用此代码通过 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='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)

但我收到此错误:

ImportError: cannot import name SignedJwtAssertionCredentials

我已经安装了 Google v3 API Python 客户端以及 OAuth2;我似乎对这些模块没有任何其他问题,尽管我没有太多使用它们。有谁知道发生了什么?

【问题讨论】:

标签: python google-api oauth-2.0


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 您是否阅读了已经发布的其他答案?请添加详细信息,解释为什么您的答案会添加现有答案中尚未包含的重要信息
    【解决方案3】:

    我试图构建一个本地开发环境,但这里的解决方案都不起作用。对我来说,难题中的额外部分是:

    $ pip install pycrypto
    

    可能除了以下任何或全部:

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

    GAE has the pycrypto package available internally(检查你的 app.yaml 中列出的库)所以需要它的东西可能在他们的机器上运行,但在你的机器上不行 - 我想 - 抱歉,我还不清楚他们让生活如此美好的原因和原因对图书馆感到很痛苦。

    【讨论】:

      【解决方案4】:

      首先检查您的oauth2client 版本。

      如果此版本 >= 2.0,则使用 ServiceAccountCredentials 而不是 SignedJwtAssertionCredentials

      看三个参考:

      【讨论】:

        【解决方案5】:

        最近更新了source repository,以使用新代码:

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

        【讨论】:

          【解决方案6】:

          看来你还没有安装 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):
          ....
          

          【讨论】:

          • 我已经安装了 PyOpenSSL (sudo pip install pyopenssl),但我仍然收到了相关错误(在 OSX 10.8.5 上使用 Python 2.7)。我的解决方法是运行sudo pip install pyopenssl --upgrade
          【解决方案7】:

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

          pip install oauth2client==1.5.2
          

          【讨论】:

          • 谢谢兄弟。经过漫长的旅程,您的答案终于有所帮助:-)
          • 正如上面提到的 michael github.com/google/oauth2client/issues/401 解释说,SignedJwtAssertionCredentials 已被删除,其行为现在在 auth2client.service_account.ServiceAccountCredentials 中实现
          • 此修复程序不再适用于 google api 客户端,因为它已更新为需要 oauthclient 2.0+。如上所述将 SignedJwtAssertionCredentials 更改为 ServiceAccountCredentials 是您想要做的。
          • 为什么这条评论不是答案。我正在使用指挥棒,我面临同样的问题,这是唯一对我有用的答案
          【解决方案8】:

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

          简单地说:pip install pyopenssl

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

          【讨论】:

          • 没有 OpenSSL Win32 安装 pyopenssl 失败并出现以下错误:'错误:仅找到不正确的 OpenSSL 目录:...'
          猜你喜欢
          • 2015-03-27
          • 1970-01-01
          • 2016-03-31
          • 2014-10-10
          • 2014-09-20
          • 2014-08-28
          • 2014-06-10
          • 2016-05-16
          • 2019-05-25
          相关资源
          最近更新 更多