QQ扫一扫联系
import base64
import json
import os
import os.path
import requests
# 请求基础API地址
API_BASE = 'https://www.example.com/api'
# 请求的API_KEY
ADMIN_API_KEY='xxxxxx'
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
def fileToBase64(file_path):
with open(file_path, "rb") as file:
file_content = file.read()
base64_content = base64.b64encode(file_content).decode('utf-8')
return base64_content
return None
def uploadFileTemp(filename):
content=fileToBase64(filename)
url = API_BASE + '/admin_api/upload/file_temp'
data = {
"ADMIN_API_KEY": ADMIN_API_KEY,
"filename": filename,
"base64Content": content
}
response = requests.post(url, data=json.dumps(data), headers=headers)
if response.status_code!=200:
raise Exception(f"请求异常:"+data["filename"])
rsp = response.json()
if rsp['code']!=0:
raise Exception(f"api返回异常:"+rsp)
return rsp['data']['path']
if __name__ == '__main__':
# 上传到临时文件
filePath = '/path/to/doc.doc'
fileTempPath = uploadFileTemp( filePath )
# 上传到文库
url = API_BASE + '/wenku_admin_api/doc/add'
data = {
"ADMIN_API_KEY": ADMIN_API_KEY,
"memberUserId": 1,
"fileUrl": fileTempPath,
"title": os.path.basename(filePath),
"cover": "",
"description": os.path.basename(filePath),
"categoryId": 12,
"catIds": "",
"creditCost": 10,
"moneyCost": '1.00',
"tags": "自营"
}
json_data=json.dumps(data)
response = requests.post(url, data=json_data, headers=headers)
rsp = response.json()
print(rsp)