【问题标题】:Invalid and/or missing SSL certificate for URL when calling apiclient.discovery.build调用 apiclient.discovery.build 时 URL 的 SSL 证书无效和/或丢失
【发布时间】:2017-05-16 05:12:24
【问题描述】:

所以我使用dev_appserver.py 在本地运行我的谷歌端点。 我使用 API Explorer 来测试应用程序。

我用来创建服务,所以我可以调用 API 的代码如下:

from apiclient.discovery import build 
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
service = build('speech', 'v1beta1', credentials=credentials)

我收到一个 SSL 错误(无效和/或缺少 SSL 证书),即使当我通过浏览器访问指定的 URL 时它工作正常(即显示绿色挂锁)。

我不确定发生了什么变化,但不久前它还可以正常工作。

我尝试禁用 SSL 检查,但未能成功。

完整日志如下:

INFO     2017-01-02 03:12:02,724 discovery.py:267] URL being requested: GET https://www.googleapis.com/discovery/v1/apis/speech/v1beta1/rest?userIp=0.2.0.3
ERROR    2017-01-02 03:12:03,022 wsgi.py:263] 
Traceback (most recent call last):
  File "/home/vini/opt/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/vini/opt/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/home/vini/opt/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/mnt/b117/home/vini/udacity/cerci-endpoint/api.py", line 28, in <module>
    service = build('speech', 'v1beta1', credentials=credentials)
  File "/mnt/b117/home/vini/udacity/cerci-endpoint/lib/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/mnt/b117/home/vini/udacity/cerci-endpoint/lib/googleapiclient/discovery.py", line 222, in build
    cache)
  File "/mnt/b117/home/vini/udacity/cerci-endpoint/lib/googleapiclient/discovery.py", line 269, in _retrieve_discovery_doc
    resp, content = http.request(actual_url)
  File "/mnt/b117/home/vini/udacity/cerci-endpoint/lib/httplib2/__init__.py", line 1609, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/mnt/b117/home/vini/udacity/cerci-endpoint/lib/httplib2/__init__.py", line 1351, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/mnt/b117/home/vini/udacity/cerci-endpoint/lib/httplib2/__init__.py", line 1307, in _conn_request
    response = conn.getresponse()
  File "/home/vini/opt/google-cloud-sdk/platform/google_appengine/google/appengine/dist27/gae_override/httplib.py", line 532, in getresponse
    raise HTTPException(str(e))
HTTPException: Invalid and/or missing SSL certificate for URL: https://www.googleapis.com/discovery/v1/apis/speech/v1beta1/rest?userIp=0.2.0.3

任何想法可能导致此问题?

我是否必须“安装”或更新 python 使用的 SSL 证书?

【问题讨论】:

  • 从今天早上开始同样遇到这个问题。可能是由于 2016 年 -> 2017 年的年份变化。

标签: python google-app-engine ssl google-api-python-client


【解决方案1】:

根据App Engine issue 13477,似乎urlfetch_cacerts.txt 中包含在App Engine Python SDK / gcloud-sdk 中的某些证书​​已过期2017-01-01

作为临时解决方法,您可以将&lt;your-cloud-sdk-path&gt;/platform/google_appengine/lib/cacerts/urlfetch_cacerts.txt 的内容替换为https://curl.haxx.se/ca/cacert.pem

【讨论】:

  • 删除声明的证书确实解决了这个问题。非常感谢。这让我发疯了。
  • 太棒了@danielx!有用。谢谢。但我很惊讶他们还没有修复它
  • 尝试从 LinkedIn API 获取时遇到同样的错误:SSLError: Invalid and/or missing SSL certificate for URL: https://api.linkedin.com/v2/ 更新 urlfetch_cacerts.txt 文件没有帮助。
  • @AndreiIvasiuc 您是否正在运行最新版本的 gcloud-sdk 并且您是否重新启动了dev_appserver.py?尝试gcloud components update 更新到最新的可用版本。
  • @danielx 不幸的是,上述方法都没有帮助。它适用于除 LinkedIn API 之外的所有内容。它之前与 LinkedIn 合作,但几天前它才开始出现此错误。
【解决方案2】:

以@danielx 为 macOS 上的用户提供的答案为基础,这对我有用。我的证书路径是:

/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cacerts/urlfetch_cacerts.txt

为了更新它,我使用了以下步骤:

cd /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cacerts
mv urlfetch_cacerts.txt urlfetch_cacerts.bup
curl -o urlfetch_cacerts.txt -k https://curl.haxx.se/ca/cacert.pem

如果您没有安装curl,您可以手动download 将证书移动到上面的文件夹中。 如果 App Engine 开发服务器已在运行,请不要忘记重新启动它。

【讨论】:

    【解决方案3】:

    最近在 2017 年 8 月在本地开发环境中遇到此错误。修复方法是更新所有 urlfetch 调用并强制验证证书:

    urlfetch.fetch(url=url, validate_certificate=True)
    

    不必接触 gcloud 证书 (MacOS)。见Issuing an HTTPS request

    【讨论】:

    • 这并没有解决我的问题。
    猜你喜欢
    • 2012-11-25
    • 2014-07-29
    • 2014-07-14
    • 1970-01-01
    • 2011-04-10
    • 2014-06-13
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多