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

时间:2014-02-15 04:37:54

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

我正在尝试使用本地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']

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

/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

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:5)

为了在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
    
  4. 对于dev_appserver,还必须在本地安装PyCrypto库,因为它不包含在SDK中。 (API客户端库也支持OpenSSL,但我假设使用PyCrypto更接近运行时环境。)

答案 1 :(得分:3)

通过在pycrypto libraries中向app.yaml部分添加libraries: - name: pycrypto version: "latest" 来修复此问题:

{{1}}