开发者社区 问答 正文

使用API更新钉盘excel文件遇到的问题

我在使用API更新钉盘excel文件时,excel文件本身顺利更新了,版本也升版了,更新时间也正确,但是直接在线点击 excel预览,还是旧版本的文件预览,如果选择下载文件,则是正确的最新版的文件,这里为什么会这样?是还需要做什么么?6ca56656-e831-460c-8e09-53cebaec7abc.png
如图,文件获得了顺利更新,但是直接在线打开文件显示的还是第一版的文件,下载没有问题,是下载的最新版。
更新excel的代码如下:

# 将新excel上传到系统里面
# 构造上传文件信息查询的POST请求
url = f"https://api.dingtalk.com/v1.0/storage/spaces/{target_space_id}/files/uploadInfos/query"
params = {
    "unionId": unionid
}
headers = {
    "Host": "api.dingtalk.com",
    "x-acs-dingtalk-access-token": access_token,
    "Content-Type": "application/json"
}
data = {
    "protocol": "HEADER_SIGNATURE",
    "multipart": False
}
try:
    response = requests.post(url, params=params, headers=headers, json=data)
    print("上传信息查询响应:", response.text)
except Exception as e:
    print(f"上传信息查询时发生错误: {e}")

# 从上传信息查询响应中获取uploadKey并保存到变量target_uploadKey中
try:
    response_json = response.json()
    target_uploadKey = response_json.get("uploadKey")
    print("获取到的uploadKey:", target_uploadKey)
except Exception as e:
    print(f"解析上传信息查询响应时发生错误: {e}")


url = response_json.get("headerSignatureInfo", {}).get("resourceUrls", [None])[0]
headers = response_json.get("headerSignatureInfo", {}).get("headers")
result = requests.put(url, data=open('docs\IVCT成品投诉newversion.xls', 'rb'), headers=headers)
print(result)

# 获取dentryUuid
# 假设 access_token 和 operator_id 已经有了
access_token = access_token
operator_id = unionid

url = f"https://api.dingtalk.com/v2.0/storage/dentries/search"
params = {
    "operatorId": operator_id
}
headers = {
    "x-acs-dingtalk-access-token": access_token,
    "Content-Type": "application/json"
}
data = {
    "keyword": "IVCT成品投诉跟进"
}

response = requests.post(url, params=params, headers=headers, json=data)
print("响应内容:", response.text)
# 从响应中获取dentryUuid列表,取第一个值并打印
try:
    response_json = response.json()
    items = response_json.get("items", [])
    if items and "dentryUuid" in items[0]:
        target_dentry_uuid = items[0]["dentryUuid"]
        print("获取到的第一个dentryUuid:", target_dentry_uuid)
    else:
        print("未找到dentryUuid")
except Exception as e:
    print(f"解析dentryUuid时发生错误: {e}")


# 提交上传文件,调用commit接口
parent_dentry_uuid = target_dentry_uuid  # 假设上面已获取到
union_id = operator_id  # 假设上面已获取到
commit_url = f"https://api.dingtalk.com/v2.0/storage/spaces/files/{parent_dentry_uuid}/commit"
commit_params = {
    "unionId": union_id
}
commit_headers = {
    "Host": "api.dingtalk.com",
    "x-acs-dingtalk-access-token": access_token,
    "Content-Type": "application/json"
}
# 获取上传文件的大小
file_path = r'docs\IVCT成品投诉newversion.xls'
file_size = os.path.getsize(file_path)
commit_data = {
    "uploadKey": target_uploadKey,
    "name": "IVCT成品投诉.xls",
    "option": {
        "size": file_size,
        "conflictStrategy": "OVERWRITE",
        "appProperties": [
            {
                "name": "自定义属性",
                "value": "属性值",
                "visibility": "PRIVATE"
            }
        ],
        "convertToOnlineDoc": False
    }
}
try:
    commit_response = requests.post(commit_url, params=commit_params, headers=commit_headers, json=commit_data)
    print("commit接口响应内容:", commit_response.text)
    commit_response_json = commit_response.json()
    print("commit接口返回JSON:", commit_response_json)
except Exception as e:
    print(f"调用commit接口时发生错误: {e}")

展开
收起
游客j6ul4vmdkcsxw 2025-09-02 11:52:50 8 分享 版权
0 条回答
写回答
取消 提交回答
问答分类: