【问题标题】:Why importing data to Zoho Analytics API causes error?为什么将数据导入 Zoho Analytics API 会导致错误?
【发布时间】:2021-01-10 19:59:29
【问题描述】:

我的目标是用 Python3 编写一个脚本,将数据推送到 Zoho Analytics 中的现有表中,该脚本将由调度程序每周使用一次。 我到目前为止所尝试的:

  1. 我可以使用 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'

  1. 我发现 Zoho Analytics 团队提供的 ReportClient Zoho Report Client for Python 与 Python 3 不兼容。因此,我从 [这里] (https://pypi.org/project/zoho-analytics-connector) 为这个 ReportClient 安装了一个包装。

  2. 根据 Zoho 网站上的示例示例和 Python3 中 Zoh​​o 包装器的 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

【问题讨论】:

标签: python-3.x zoho rest


【解决方案1】:

2021 年 2 月,我在我的库中更改了这个继承的 Zoho 代码。 现在它是: contHeader = urlResp.headers.get("Content-Type",None)

这避免了您遇到的最终异常。

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 2020-11-05
    • 2019-08-31
    • 2020-11-16
    • 2012-05-04
    • 2020-07-16
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多