Python 调用apiclient.discovery.build时URL的SSL证书无效和/或丢失

Python 调用apiclient.discovery.build时URL的SSL证书无效和/或丢失,python,google-app-engine,ssl,google-api-python-client,Python,Google App Engine,Ssl,Google Api Python Client,因此,我使用dev_appserver.py在本地运行我的google端点。 我使用API资源管理器来测试应用程序 我用于创建服务的代码如下所示,以便调用API: from apiclient.discovery import build from oauth2client.client import GoogleCredentials credentials = GoogleCredentials.get_application_default() service = build('spee

因此,我使用
dev_appserver.py
在本地运行我的google端点。 我使用API资源管理器来测试应用程序

我用于创建服务的代码如下所示,以便调用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
INFO 2017-01-02 03:12:02724 discovery.py:267]请求的URL:获取https://www.googleapis.com/discovery/v1/apis/speech/v1beta1/rest?userIp=0.2.0.3
错误2017-01-02 03:12:03022 wsgi.py:263]
回溯(最近一次呼叫最后一次):
文件“/home/vini/opt/google cloud sdk/platform/google_appengine/google/appengine/runtime/wsgi.py”,第240行,在Handle中
handler=\u config\u handle.add\u wsgi\u中间件(self.\u LoadHandler())
LoadHandler中的文件“/home/vini/opt/google cloud sdk/platform/google\u appengine/google/appengine/runtime/wsgi.py”,第299行
处理程序,路径,err=LoadObject(self.\u处理程序)
LoadObject中的文件“/home/vini/opt/google cloud sdk/platform/google\u appengine/google/appengine/runtime/wsgi.py”,第85行
obj=\uuuuuuuuuuuuuu导入(路径[0])
文件“/mnt/b117/home/vini/udacity/cerci endpoint/api.py”,第28行,在
服务=构建('speech','v1beta1',凭证=凭证)
位置包装中的文件“/mnt/b117/home/vini/udacity/cerci endpoint/lib/oauth2client/_helpers.py”,第133行
已包装退货(*args,**kwargs)
文件“/mnt/b117/home/vini/udacity/cerci endpoint/lib/googleapiclient/discovery.py”,第222行,内部版本
缓存)
文件“/mnt/b117/home/vini/udacity/cerci endpoint/lib/googleapiclient/discovery.py”,第269行,位于检索发现文件中
resp,content=http.request(实际url)
请求中的文件“/mnt/b117/home/vini/udacity/cerci endpoint/lib/httplib2/_init__.py”,第1609行
(响应,内容)=self.\u请求(conn,authority,uri,request\u uri,method,body,header,重定向,cachekey)
文件“/mnt/b117/home/vini/udacity/cerci endpoint/lib/httplib2/_init__.py”,第1351行,在请求中
(响应,内容)=自连接请求(连接,请求uri,方法,正文,标题)
文件“/mnt/b117/home/vini/udacity/cerci endpoint/lib/httplib2/_init__.py”,第1307行,在连接请求中
response=conn.getresponse()
文件“/home/vini/opt/google cloud sdk/platform/google\u appengine/google/appengine/dist27/gae\u override/httplib.py”,第532行,在getresponse中
引发HTTPException(str(e))
HTTPException:URL的SSL证书无效和/或丢失:https://www.googleapis.com/discovery/v1/apis/speech/v1beta1/rest?userIp=0.2.0.3
你知道是什么导致了这个问题吗

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

根据
urlfetch\u cacerts.txt
中的一些证书似乎已过期
App Engine python SDK
/
gcloud SDK


作为临时解决办法,您可以将
/platform/google\u appengine/lib/cacerts/urlfetch\u cacerts.txt的内容替换为最近在2017年8月在本地开发环境中出现此错误。修复方法是更新所有urlfetch调用并强制验证证书:

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

无需触摸gcloud证书(MacOS)。请参阅。

根据@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
,可以手动将证书移动到上面的文件夹中。
如果App Engine dev服务器已经运行,请不要忘记重新启动它。

从今天早上开始出现此问题也是一样的。可能是由于2016->2017年的年份变化。删除声明的证书确实解决了问题。非常感谢你。这简直让我发疯。太棒了@danielx!它起作用了。谢谢但我很惊讶他们没有修复它,因为尝试从LinkedIn API获取时出现了相同的错误:
SSLError:URL的SSL证书无效和/或丢失:https://api.linkedin.com/v2/
更新
urlfetch\u cacerts.txt
文件没有帮助。@AndreiIvasiuc您正在运行最新版本的gcloud sdk,并且重新启动了吗
dev_appserver.py
?请尝试
gcloud components update
以更新到可用的最新版本。@danielx遗憾的是,上述方法均无效。除了LinkedIn API之外,它可以与任何东西一起工作。它以前和LinkedIn一起工作,但几天前它刚刚开始出现这个错误。这并没有解决我的问题。