遇到的问题
python调用gitee上的API上传附件到仓库指定 Release失败
问题描述
通过调用gitee上的“上传附件到仓库指定 Release”的API提供的方式https://gitee.com/api/v5/repos/{owner}/{repo}/releases/{release_id}/attach_files
上传失败,在API的页面测试失败,调用方式也失败。
尝试的方案
owner = '***' # 替换为你的Gitee用户名
repo = '****' # 替换为你的仓库名
access_token = '****' # 替换为你的Gitee访问令牌
release_id = find_latest_release(owner, repo, access_token)
headers = {
'Content-Type': 'application/json;charset=UTF-8'
}
file_path = f"D:/output.zip.001"
assets_url = f"https://gitee.com/api/v5/repos/{owner}/{repo}/releases/{release_id}/attach_files?access_token={access_token}"
# Release信息
data_info = {
"access_token": access_token, # 口令
"file": file_path
}
# 打开文件并读取内容
with open(file_path, 'rb') as file:
# 发送POST请求上传文件
response = requests.post(assets_url, headers=headers, files={'file': file})
# 检查响应状态并返回结果
if response.status_code == 201:
print("File uploaded successfully!")
return response.json() # 返回创建成功的Release信息
else:
print("Failed to upload file, status code:", response.status_code)
return None