代码在 Google Colab 中运行完美,但在 Jupyter Notebook 中出现错误。
第一个错误是
超过截止日期:504 超过截止日期
然后我分配了我的公司代理
os.environ['HTTPS_PROXY'] = proxy
并得到了新的错误
google.api_core.exceptions.ServiceUnavailable: 503 无法连接到所有地址;最后一个错误:未知:ipv4:192.168.152.200:8080:Ssl握手失败:SSL_ERROR_SSL:错误:1000007d:SSL例程:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
为了能够检查更改,我首先运行了另一个没有
os.environ['HTTPS_PROXY'] = proxy
的代码:
import httplib2
http = httplib2.Http()
resp, content = http.request(
'https://www.googleapis.com/robot/v1/metadata/x509/pythonaccess%***.iam.gserviceaccount.com', 'GET')
print(resp)
print(content)
错误是(TimeoutError:[WinError 10060])。
然后我为新代码写了
os.environ['HTTPS_PROXY']" = proxy
。
发生另一个错误:
ssl.SSLCertVerificationError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败:无法获取本地颁发者证书(_ssl.c:1002)
但是当我添加
http = httplib2.Http(disable_ssl_certificate_validation=True)
代码运行完美。不幸的是,我不知道如何在
client = BetaAnalyticsDataClient()
中为 Google api (google.analytics.data_v1beta.types) 禁用 SSL 证书验证,但我确信 ssl 存在问题。
尝试添加路径
os.environ['GRPC_DEFAULT_SSL_ROOTS_FILE_PATH']
os.environ['CURL_CA_BUNDLE']
甚至
os.environ['GOOGLE_API_USE_CLIENT_CERTIFICATE']
没有任何结果。
请帮忙!
公司有代理和杀毒软件。
完整请求:
import pandas as pd
import numpy as np
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import DateRange
from google.analytics.data_v1beta.types import Dimension
from google.analytics.data_v1beta.types import Metric
from google.analytics.data_v1beta.types import RunReportRequest
from google.analytics.data_v1beta.types import OrderBy
os.environ['https_proxy'] = "http://192.168.152.200:8080"
proxy = "http://192.168.152.200:8080"
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'D:\***\scripts\credentials\***f.json'
property_id = '***'
client = BetaAnalyticsDataClient()
def format_report(request):
response = client.run_report(request)
# Row index
row_index_names = [header.name for header in response.dimension_headers]
row_header = []
for i in range(len(row_index_names)):
row_header.append([row.dimension_values[i].value for row in response.rows])
row_index_named = pd.MultiIndex.from_arrays(np.array(row_header), names = np.array(row_index_names))
# Row flat data
metric_names = [header.name for header in response.metric_headers]
data_values = []
for i in range(len(metric_names)):
data_values.append([row.metric_values[i].value for row in response.rows])
output = pd.DataFrame(data = np.transpose(np.array(data_values, dtype = 'f')),
index = row_index_named, columns = metric_names)
return output
request = RunReportRequest(
property='properties/'+property_id,
dimensions=[Dimension(name="month"),
Dimension(name="sessionMedium")],
metrics=[Metric(name="averageSessionDuration"),
Metric(name="activeUsers")],
order_bys = [OrderBy(dimension = {'dimension_name': 'month'}),
OrderBy(dimension = {'dimension_name': 'sessionMedium'})],
date_ranges=[DateRange(start_date="2023-01-01", end_date="today")],
)
format_report(request)
我的情况也有同样的问题。但我找不到解决办法。 谁能解决这个问题。