import requests
import certifi
r = requests.get('https://github.com/')
print(r)
这首先导致了一个SSL连接错误。经过广泛的搜索和我们IT部门的帮助,我终于解决了这个问题。解决方案是将公司的根证书添加到certifi证书存储中。
现在对于其他请求,不幸的是我仍然遇到相同的问题。例如,使用google2pandas包调用Google Analytics API的示例代码:
from google2pandas import *
query = {
'reportRequests': [{
'viewId' : 37616054,
'dateRanges': [{
'startDate' : '8daysAgo',
'endDate' : 'today'}],
'dimensions' : [
{'name' : 'ga:date'},
{'name' : 'ga:pagePath'},
{'name' : 'ga:browser'}],
'metrics' : [
{'expression' : 'ga:pageviews'}],
'dimensionFilterClauses' : [{
'operator' : 'AND',
'filters' : [
{'dimensionName' : 'ga:browser',
'operator' : 'REGEXP',
'expressions' : ['Firefox']},
{'dimensionName' : 'ga:pagePath',
'operator' : 'REGEXP',
'expressions' : ['iPhone']}]
}]
}]
}
# Assume we have placed our client_secrets_v4.json file in the current
# working directory.
conn = GoogleAnalyticsQueryV4(secrets='Analytics.json')
df = conn.execute_query(query)
这里我仍然遇到了之前简单调用时的SSL错误:
我相信有另一个正在使用的库,它不依赖于certifi。但是我不知道在哪里以及如何添加我的根证书,以便所有的iPython请求都能正常工作。C:\ProgramData\Anaconda3\lib\ssl.py中的_create方法,参数(sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session) 848 # 非阻塞 849 raise ValueError("对于非阻塞套接字,不应指定do_handshake_on_connect") --> 850 self.do_handshake() 851 except (OSError, ValueError): 852 self.close()
C:\ProgramData\Anaconda3\lib\ssl.py中的do_handshake方法,参数(block)
1106 if timeout == 0.0 and block: 1107
self.settimeout(None) -> 1108 self._sslobj.do_handshake() 1109 finally: 1110 self.settimeout(timeout)SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书(_ssl.c:1045)
欢迎提供任何想法。