首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >httplib2证书验证失败

httplib2证书验证失败
EN

Stack Overflow用户
提问于 2015-01-07 10:00:32
回答 2查看 9.7K关注 0票数 2

我已经试过所有我能找到的让它起作用..。

我正在为一个基于python的任务程序(称为GTG)开发一个插件。我在Opensuse Linux上运行Gnome。

代码(Python2.7):

代码语言:javascript
复制
def initialize(self):
    """
    Intialize backend: try to authenticate. If it fails, request an authorization.
    """
    super(Backend, self).initialize()
    path = os.path.join(CoreConfig().get_data_dir(), 'backends/gtask', 'storage_file-%s' % self.get_id())
    # Try to create leading directories that path
    path_dir = os.path.dirname(path)
    if not os.path.isdir(path_dir):
        os.makedirs(path_dir)

    self.storage = Storage(path)
    self.authenticate()

def authenticate(self):
    """ Try to authenticate by already existing credences or request an authorization """
    self.authenticated = False

    credentials = self.storage.get()
    if credentials is None or credentials.invalid == True:
        self.request_authorization()
    else:
        self.apply_credentials(credentials)
        # Request periodic import, avoid waiting a long time
        # self.start_get_tasks()

def apply_credentials(self, credentials):
    """ Finish authentication or request for an authorization by applying the credentials """


    http = httplib2.Http(ca_certs = '/etc/ssl/certs/ca_certs.pem', disable_ssl_certificate_validation=True)

    http = credentials.authorize(http)

    # Build a service object for interacting with the API.
    self.service = build_service(serviceName='tasks', version='v1', http=http, developerKey='AIzaSyAmUlk8_iv-rYDEcJ2NyeC_KVPNkrsGcqU')
    # self.service = build_service(serviceName='tasks', version='v1')
    self.authenticated = True

def _authorization_step2(self, code):
    credentials = self.flow.step2_exchange(code)
    # credential = self.flow.step2_exchange(code)

    self.storage.put(credentials)
    credentials.set_store(self.storage)

    return credentials

def request_authorization(self):
    """ Make the first step of authorization and open URL for allowing the access """

    self.flow = OAuth2WebServerFlow(client_id=self.CLIENT_ID,
        client_secret=self.CLIENT_SECRET,
        scope='https://www.googleapis.com/auth/tasks',
        redirect_uri='http://localhost:8080',
        user_agent='GTG')


    oauth_callback = 'oob'
    auth_uri = self.flow.step1_get_authorize_url(oauth_callback)
    # credentials = self.flow.step2_exchange(code)



    # url = self.flow.step1_get_authorize_url(oauth_callback)
    browser_thread = threading.Thread(target=lambda: webbrowser.open_new(auth_uri))
    browser_thread.daemon = True
    browser_thread.start()

    # Request the code from user
    BackendSignals().interaction_requested(self.get_id(), _(
        "You need to <b>authorize GTG</b> to access your tasks on <b>Google</b>.\n"
        "<b>Check your browser</b>, and follow the steps there.\n"
        "When you are done, press 'Continue'."),
        BackendSignals().INTERACTION_TEXT,
        "on_authentication_step")


def on_authentication_step(self, step_type="", code=""):

    if step_type == "get_ui_dialog_text":
        return _("Code request"), _("Paste the code Google has given you"
                "here")
    elif step_type == "set_text":
        try:

            credentials = self._authorization_step2(code)
        except FlowExchangeError, e:
            # Show an error to user and end
            self.quit(disable = True)
            BackendSignals().backend_failed(self.get_id(), 
                        BackendSignals.ERRNO_AUTHENTICATION)
            return

        self.apply_credentials(credentials)
        # Request periodic import, avoid waiting a long time
        self.start_get_tasks()

浏览器窗口打开,我收到了来自Google的代码。该程序打开一个小窗口,在这里我可以从Google.When输入代码,这是在控制台中得到的:

代码语言:javascript
复制
 No handlers could be found for logger "oauth2client.util"
Created new window in existing browser session.
[522:549:0108/063825:ERROR:nss_util.cc(821)] After loading Root Certs, loaded==false: NSS error code: -8018

但SSL图标在Chrome中是绿色的..。

然后,当我提交代码时,我得到:

代码语言:javascript
复制
    Exception in thread Thread-10:
Traceback (most recent call last):
  File "/usr/lib64/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib64/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/site-packages/GTG/backends/backend_gtask.py", line 204, in on_authentication_step
    credentials = self._authorization_step2(code)
  File "/usr/lib/python2.7/site-packages/GTG/backends/backend_gtask.py", line 151, in _authorization_step2
    credentials = self.flow.step2_exchange(code)
  File "/usr/lib/python2.7/site-packages/oauth2client/util.py", line 132, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/oauth2client/client.py", line 1283, in step2_exchange
    headers=headers)
  File "/usr/lib/python2.7/site-packages/httplib2/__init__.py", line 1586, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/lib/python2.7/site-packages/httplib2/__init__.py", line 1328, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/lib/python2.7/site-packages/httplib2/__init__.py", line 1250, in _conn_request
    conn.connect()
  File "/usr/lib/python2.7/site-packages/httplib2/__init__.py", line 1037, in connect
    raise SSLHandshakeError(e)
SSLHandshakeError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)

文件名为backend_gtask.py..。

我已经尝试导入证书,如下所述:How to update cacerts.txt of httplib2 for Github?

我已经尝试过禁用验证(httplib2.Http(disable_ssl_certificate_validation=True)),如网上所述,

我更新了python包(这似乎使事情变得更糟)

我在/etc/ssl之间来回复制ca_certs.pem .和/usr/lib/python2.7/.

当我在浏览器中访问auth页面时,它说证书是被验证的.

我还能查到什么?

短测试代码:

代码语言:javascript
复制
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from oauth2client.file import Storage

CLIENT_ID = 'id'
CLIENT_SECRET = 'secret'

flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
                           client_secret=CLIENT_SECRET,
                           scope='https://www.googleapis.com/auth/tasks',
                           redirect_uri='http://localhost:8080')

storage = Storage('creds.data')

credentials = run(flow, storage)

print "access_token: %s" % credentials.access_token

在此发现:https://github.com/burnash/gspread/wiki/How-to-get-OAuth-access-token-in-console%3F

EN

回答 2

Stack Overflow用户

发布于 2015-01-09 08:59:33

好的..。多亏了Steffen Ullrich。

httplib2版本0.9尝试使用系统证书,而不是过去随附的certs.txt文件。它还加强了核查。

httplib2可以使用几个有用的参数--特别是ca_certs。使用它来指向您的ssl安装中的实际*.pem文件。我不能是一个文件夹,必须是一个真正的文件。

在插件的初始化过程中,我使用以下方法:

代码语言:javascript
复制
self.http = httplib2.Http(ca_certs = '/etc/ssl/ca-bundle.pem')

然后,对于对httplib或google客户端库的所有后续调用,我将预构建的http对象作为如下参数传递:

代码语言:javascript
复制
credentials = self.flow.step2_exchange(code, self.http)

self.http = credentials.authorize(self.http)

现在ssl连接使用新的httplib2..。

我最终必须确保插件能够在任何系统上找到证书,但至少我知道问题出在哪里。

再次感谢Steffen Ullrich带我走过这段时间。

票数 2
EN

Stack Overflow用户

发布于 2022-08-26 21:21:39

请参阅this answer以获得更简单的修补程序,而无需接触代码:只需在环境变量中设置证书包pem文件路径:

代码语言:javascript
复制
export HTTPLIB2_CA_CERTS="\path\to\your\ca-bundle"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27816814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档