前天一哥一个客户定制了款脚本,读取账号和存储脚本运行结果的记录需要存放到坚果云的云盘中。然而坚果云官方未提供API接口,就废了一番功夫。
先是easyclick在坚果云中读取账号信息:
jianguoyun={} jianguoyun.host="https://dav.jianguoyun.com/dav/" jianguoyun.read=function (files) { const CONFIG = { "path": "mydata/", //坚果云文件目录 "scriptName":files+".txt",//文件名请更改成您的 "user": "[email protected]", //坚果云应用的账号 "key": "hjfhjweohjhfojkhewh", //坚果云的应用密钥 } headers= { "Authorization": "Basic " + java.lang.String(android.util.Base64.encode(java.lang.String(CONFIG.user +':' + CONFIG.key).getBytes(), 2)), "Content-Type": "text/plain;charset=UTF-8", "Connection": "Keep-Alive", "Accept-Encoding": "gzip", "User-Agent": "okhttp/3.12.1" } pm={ "url":jianguoyun.host+CONFIG.path+CONFIG.scriptName, "method":"GET", "header":headers } try { let result=http.request(pm) if (result.statusCode===200) { logd("读取坚果云: "+CONFIG.scriptName+" 成功") return result.body }else { return false } }catch (e) { logd(e.message) return false } }
jianguoyun.updata=function (files,STR) { const CONFIG = { "path": "mydata/", //坚果云文件目录 "scriptName":files+".txt",//文件名请更改成您的 "user": "[email protected]", //坚果云应用的账号 "key": "hjfhjweohjhfojkhewh", //坚果云的应用密钥 } headers= { "Authorization": "Basic " + java.lang.String(android.util.Base64.encode(java.lang.String(CONFIG.user +':' + CONFIG.key).getBytes(), 2)), "Content-Type": "text/plain;charset=UTF-8", "Connection": "Keep-Alive", "Accept-Encoding": "gzip", "User-Agent": "okhttp/3.12.1" } pm={ "url":jianguoyun.host+CONFIG.path+CONFIG.scriptName, "method":"PUT", "header":headers, "requestBody":STR, "timeout":30*1000, } try { sleep(300) let result=http.request(pm) if (result.statusCode===200||result.statusCode==204) { logd("坚果云更新: "+CONFIG.scriptName +" 成功") return true }else { logd(result.statusCode); return false } }catch (e) { logd(e.message) return false } }
jianguoyun.read("X1.txt") //测试读取: X1.txt 文档内容