一、获取token鉴权
官方接口说明:
- POST api/2.0/authentication
- 相关参数 UserName:用户名或电子邮件
Password:密码 - 利用Python调用相关接口
url= "yourportal.onlyoffice.com/api/2.0/authentication"
au_data = {
"UserName":"xxxx",
"Password":"xxxx"
}
hearders = {
'Content-Type':'application/json',
'Accept':'application/json'
}
response = request.post(url,hearders=hearders,data=json.dumps(au_data))
成功返回值状态码为201,通过返回的response获取token以及token有效时长
二、获取我的文档部分的文件夹id,将文件上传到该id的文件夹中
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
body_data = {
"UserId":userName,
"filterType": "None",
"searchInContent": true,
"withsubfolders": true,
"applyFilterOption": "None"
}
url= "yourportal.onlyoffice.com/api/2.0/files/@my"
response = requests.get(url,headers=headers,data=json.dumps(body_data))
成功返回状态码值为200,通过返回的response获取folder_id
三、将文件上传至我的文档并获取文件的外部链接
url = "yourportal.onlyoffice.com/api/2.0/files/要上传的文件夹id(替换成第二步获取到的id)/upload/create_session.json"
headers={
"Content-Type":"application/json",
"Authorization": f"Bearer {token}"
}
body={
"CreateOn":上传文件的时间(使用datetime获取现在时间),
"FileName":文件名,
"FileSize":文件大小,
"folderId":上传到我的文档的文件夹id(第二步获取)
}
response = requests.post(url,headers=headers,data=json.dumps(body))
成功返回状态码值为201,从reponse中获取上传的文件id用于获取该文件的外部链接
四、获取文件外部链接
url= "yourportal.onlyoffice.com/api/2.0/files/file/文件id/share"
headers={
"Content-Type":"application/json",
"Accept":"application/json",
"Authorization": f"Bearer {token}"
}
response = requests.get(url,headers=headers)
从response中获取文件的外部链接。注意:该接口和官方文档略微不同
具体参考onlyOffice官方文档说明onlyOffice接口说明文档
初次使用在此记录