问题描述
我的目标是用python3编写一个脚本,该脚本会将数据推入Zoho Analytics中的现有表中,该脚本将每周由调度程序使用一次。 我已经尝试过的东西:
- 我可以使用cURL命令成功导入一些数据。像这样
curl -X POST \ 'https://analyticsapi.zoho.com/apI/OwnerEmail/Workspace/TableName?ZOHO_ACTION=IMPORT& ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_VERSION=1.0&ZOHO_IMPORT_TYPE=APPEND&ZOHO_AUTO_IDENTIFY=True&ZOHO_ON_IMPORT_ERROR=ABORT&ZOHO_CREATE_TABLE=False' \ -H 'Authorization: Zoho-oauthtoken *******' \ -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \ -F ZOHO_FILE='path_to_csv'
-
我发现Zoho Analytics团队提供的ReportClient Zoho Report Client for Python与Python 3不兼容。因此,我从[here](https://pypi.org/project/zoho-analytics-connector)安装了此ReportClient的包装。
-
以下是来自Zoho网站的示例示例,并在python3中针对Zoho的包装器的github中进行了测试,我实现了以下内容: 有一堂课来保存我的ENV变量
import os
from zoho_analytics_connector.report_client import ReportClient,ServerError
from zoho_analytics_connector.enhanced_report_client import EnhancedZohoAnalyticsClient
class ZohoTracking:
LOGINEMAILID = os.getenv("ZOHOANALYTICS_LOGINEMAIL")
REFRESHTOKEN = os.getenv("ZOHOANALYTICS_REFRESHTOKEN")
CLIENTID = os.getenv("ZOHOANALYTICS_CLIENTID")
CLIENTSECRET = os.getenv("ZOHOANALYTICS_CLIENTSECRET")
DATABASENAME = os.getenv("ZOHOANALYTICS_DATABASENAME")
OAUTH = True
TABLENAME = "My Table"
实例化客户端类
def get_enhanced_zoho_analytics_client(self) -> EnhancedZohoAnalyticsClient:
assert (not self.OAUTH and self.AUTHTOKEN) or (self.OAUTH and self.REFRESHTOKEN)
rc = EnhancedZohoAnalyticsClient(
// Just setting email,token,etc using class above
...
)
return rc```
Then have a method to upload data to existing table,the data_upload() function has the problem.
def enhanced_data_upload(self):
enhanced_client = self.get_enhanced_zoho_analytics_client()
try:
with open("./import/tracking3.csv","r") as f:
import_content = f.read()
print(type(import_content))
except Exception as e:
print(f"Error:Check if file exists in the import directory {str(e)}")
return
res = enhanced_client.data_upload(import_content=import_content,table_name=ZohoTracking.TABLENAME)
assert res
Traceback (most recent call last):
File "push2zoho.py",line 106,in <module>
sample.enhanced_data_upload()
File "push2zoho.py",line 100,in enhanced_data_upload
res = enhanced_client.data_upload(import_content=import_content,table_name=ZohoTracking.TABLENAME)
File "/Users/.../zoho_analytics_connector/enhanced_report_client.py",line 99,in data_upload
matching_columns=matching_columns)
File "/Users/.../site-packages/zoho_analytics_connector/report_client.py",line 464,in importData_v2
r=self.__sendRequest(url=url,httpMethod="POST",payLoad=payload,action="IMPORT",callBackData=None)
File "/Users/.../zoho_analytics_connector/report_client.py",line 165,in __sendRequest
raise ServerError(respObj)
File "/Users/.../zoho_analytics_connector/report_client.py",line 1830,in __init__
contHeader = urlResp.headers["Content-Type"]
TypeError: 'nonetype' object is not subscriptable
That is the error I receive. What am I missing in this puzzle? Help is appreciated
解决方法
在 2021 年 2 月,我更改了我库中继承的 Zoho 代码。 现在它是: contHeader = urlResp.headers.get("Content-Type",None)
这避免了您遇到的最后一个异常。