ImportError:无法导入名称SignedJwtAssertionCredentials

use*_*661 30 python google-api oauth-2.0

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

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)
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

ImportError: cannot import name SignedJwtAssertionCredentials
Run Code Online (Sandbox Code Playgroud)

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

Loc*_*ane 68

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

pip install oauth2client==1.5.2
Run Code Online (Sandbox Code Playgroud)

  • 问题解释如下:https://github.com/google/oauth2client/issues/401 (8认同)
  • 正如迈克尔·上面提到https://github.com/google/oauth2client/issues/401解释说SignedJwtAssertionCredentials已被删除,其行为现在是auth2client.service_account.ServiceAccountCredentials实施 (5认同)
  • 此修复程序不再适用于google api客户端,因为它已更新为需要oauthclient 2.0+.如上所述,将SignedJwtAssertionCredentials更改为ServiceAccountCredentials是您要执行的操作. (3认同)
  • 多谢兄弟.经过漫长的旅程,你的答案终于有了帮助:-) (2认同)

小智 22

好像你没有安装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):
....
Run Code Online (Sandbox Code Playgroud)

  • 我安装了PyOpenSSL(`sudo pip install pyopenssl`),我仍然*收到了有问题的错误(在OSX 10.8.5上使用Python 2.7).我的修复是运行`sudo pip install pyopenssl --upgrade`. (11认同)

Mat*_*att 6

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

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

...
Run Code Online (Sandbox Code Playgroud)