没有名为OpenSSL.crypto和ImportError的模块:SignedJwtAssertionCredentials

2024-05-23 22:20:38 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

但我需要“签名的jwtassertioncredentials”!所以我进一步隔离了这个错误,发现(在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']

无论如何,将"/usr/local/lib/python2.7/site-packages/OpenSSL"添加到sys.path或/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

有人能帮忙吗?


Tags: pytestapplibusrlocalgooglecontents
2条回答

通过将pycrypto添加到我的app.yaml中的libraries部分修复了此问题:

libraries:

- name: pycrypto
  version: "latest"

要在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客户端库也支持OpenSSL,但我假设使用PyCrypto更接近运行时环境。)

相关问题 更多 >