【问题标题】:No module named OpenSSL.crypto and ImportError: SignedJwtAssertionCredentials没有名为 OpenSSL.crypto 和 ImportError 的模块:SignedJwtAssertionCredentials
【发布时间】:2014-03-14 15:31:50
【问题描述】:

我正在尝试使用本地 dev_appserver 在本地连接到 BigQuery API,遵循本教程:https://developers.google.com/bigquery/authorization?hl=de#service-accounts 运行本网站上提到的代码,返回 ImportError:

ImportError: cannot import name SignedJwtAssertionCredentials

所以我跟踪错误并发现(在 oauth2client/client.py 中):

if HAS_CRYPTO:
  # PyOpenSSL and PyCrypto are not prerequisites for oauth2client, so if it is
  # missing then don't create the SignedJwtAssertionCredentials or the
  # verify_id_token() method.

  class SignedJwtAssertionCredentials(AssertionCredentials):

但我需要“SignedJwtAssertionCredentials”!所以我进一步隔离了错误并发现(在 oauth2client/crypt.py 中)这条线实际上导致了这个问题:

from OpenSSL import crypto

我试过了:

$ python
>>> import OpenSSL
>>> OpenSSL.__file__
'/usr/local/lib/python2.7/site-packages/OpenSSL/__init__.pyc'
>>> from OpenSSL import crypto
>>> crypto.__file__
'/usr/local/lib/python2.7/site-packages/OpenSSL/crypto.so'

看起来很有希望,还检查了我的代码的 sys.path:

['/Users/mattes/Developer/gae-projects/project123', 
 '/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine', 
 '/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine', 
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
 '/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2', 
'/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/protorpc-1.0', 
'/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob-1.1.1', 
'/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/yaml-3.10']

无论如何,在 sys.path 中添加 "/usr/local/lib/python2.7/site-packages/OpenSSL" 或在 /Users/mattes/Developer/gae-projects/project123 下符号链接 /usr/local/lib/python2.7/site-packages/OpenSSL 都不能解决此问题。

/usr/local/lib/python2.7/site-packages/OpenSSL 看起来像:

├── SSL.so
├── __init__.py
├── __init__.pyc
├── crypto.so
├── rand.so
├── test
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── test_crypto.py
│   ├── test_crypto.pyc
│   ├── test_rand.py
│   ├── test_rand.pyc
│   ├── test_ssl.py
│   ├── test_ssl.pyc
│   ├── util.py
│   └── util.pyc
├── tsafe.py
├── tsafe.pyc
├── version.py
└── version.pyc

使用 Mac 10.9 Mavericks、Python 2.7.5

有人可以帮忙吗?

【问题讨论】:

标签: python google-app-engine google-oauth google-bigquery


【解决方案1】:

通过在我的app.yaml 中将pycrypto 添加到libraries 部分来解决此问题:

libraries:

- name: pycrypto
  version: "latest"

【讨论】:

  • 将 pycrypto 添加到 app.yaml 有什么作用?它是否强制更新 Python OpenSSL 包装器?
【解决方案2】:

为了在 GAE 服务器上运行它,我发现需要三个步骤:

  1. 安装最新版本的Google API Client(或至少安装 oauth2client 模块)。请注意,它们提供了以 GAE 为目标的下载。

  2. 将我的 .p12 密钥文件转换为 .pem 格式(使用 openssl 命令行工具)

    openssl pkcs12  -nocerts -in cert.p12 -out cert.pem
    
  3. 将 PyCrypto 库添加到 app.yaml。

    libraries:
      - name: pycrypto
        version: "2.6"  # this could be "latest" if you are daring
    

对于 dev_appserver,还需要在本地安装 PyCrypto 库,因为它不包含在 SDK 中。 (API Client 库也支持 OpenSSL,但我假设使用 PyCrypto 更接近运行时环境。)

【讨论】:

  • 对我来说,pycparser/__init__.py,作为PyCrypto 的依赖项包含在尝试导入subprocess.Popen,并且在App Engine python 环境中失败。这导致HAS_CRYPTO 签入oauth2client/client.py 失败,因此未定义SignedJwtAssertionCredentials。 (我使用的是 Mac。)
  • @EricWalker 我在 Mac 上遇到了和你一样的问题。你解决了吗?
  • @poiuytrez,我已经有一段时间没有处理这个问题了,但我记得终于让它工作了。我不记得我做了什么。
猜你喜欢
  • 2013-03-11
  • 2012-12-07
  • 2012-05-23
  • 2019-07-29
  • 2015-07-04
  • 2014-03-15
  • 2017-12-29
  • 2014-09-12
  • 2017-05-09
相关资源
最近更新 更多