ImportError:无法导入名称SignedJwtAssertionCredentials

时间:2012-12-28 00:33:58

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

然而我收到了这个错误:

ImportError: cannot import name SignedJwtAssertionCredentials

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

8 个答案:

答案 0 :(得分:65)

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

def freeze_index(term, array)
original_array = array
if term.is_a?(Fixnum)
    frozen_terms = array[term]
    #Delete all the 'frozen' terms
    original_array.delete_at(term)
    original_array.rotate!
    # insert the 'frozen' terms back into their original spots
    original_array = original_array.insert(term, frozen_terms)
elsif term.is_a?(Range)
    # convert the range into an array to make pulling the integers out of it easier
    term_array = term.to_a
    n = 0

    frozen_terms = []

    #put the frozen terms into an array
    term.each {|index| frozen_terms << array[index]}

    #make the frozen terms equal to nil
    term_array.each { |index| original_array[index] = nil}
    #get rid of the nils and rotate the remaining terms
    original_array.compact!.rotate!

    # go through all the frozen terms and insert them back into their original places
    frozen_terms.each do |item|
        original_array = original_array.insert(term_array[n], item )
        n += 1
    end
    original_array
end
end


a = ['a','b','c','d','e']
freeze_index((0..2), a)

答案 1 :(得分: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):
....

答案 2 :(得分:6)

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

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

...

答案 3 :(得分:4)

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

简单地说:pip install pyopenssl

请记住:如果您没有安装OpenSSL Win32库http://slproweb.com/products/Win32OpenSSL.html,它将在Windows上失败(您需要完整的软件包,而不是简易版)。另外请记住,在安装pyopenssl

之前,需要将它添加到路径var中

答案 4 :(得分: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中列出的库)所以需要它的东西可能在他们的机器上工作但不在你的机器上 - 我想 - 抱歉我还不清楚它们是什么以及为什么制造生活如此悲惨的图书馆。

答案 5 :(得分:2)

首先检查您的oauth2client版本。

如果此版本&gt; = 2.0,请使用ServiceAccountCredentials代替SignedJwtAssertionCredentials

看看三个参考:

答案 6 :(得分:1)

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

答案 7 :(得分:0)

您可以尝试使用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')