示例代码

下面是用 lua 写的 API 示例。 本代码由 天朝五毛君 分享。 触动专业版 iOS 无需写入 key,无需写入验证串 auth。

local sz = require"sz"
local http = require"szocket.http"
local json = sz.json
local ltn12 = require"ltn12"
local response_body = {} -- 构造主体部位接受的地方
local post_data  = {
    --触动专业版 iOS 无需写入 key
    action = "getAuth",
    key = "JqYVQgWc4V6dTVq部分马赛克处理掉7yKRbZDGYfg4Yp6F4qv0xjoU",  -- 这就不用我自己的了……替换成自己的就好了
    devices = {"aab9d7903部分马赛克处理掉cc0c0f51b595b70"},  -- 设备 ID
    time = os.time(),
    valid = 3600,
}
post_data = json.encode(post_data)    
local staus,code,header = http.request{
    url = "http://openapi.touchsprite.com/api/openapi",
    method = "POST",
    headers =
    {
        ["Content-Type"] = "application/x-www-form-urlencoded",
        ["Content-Length"] = #post_data,
    },
    source = ltn12.source.string(post_data),
    sink = ltn12.sink.table(response_body),  --用 ltn12 转化接受的数据为 lua 可用
}
dialog(table.concat(response_body))

--将会打印以下内容,这是一个 json,将之使用 json.decode 处理后取 auth 字段保存起来供我们接下来使用
--{"status":200,
--触动专业版 iOS 无需写入验证串
--"auth":"直接马赛克好了,那么长的一串",这就是我们获取到的验证串,由于太长我就马赛克掉了
--"valid":2592000,"devices":["aab9d7903393a992dcc0c0f51b595b70"],
--"remainder_token":0,
--"time":1470216706}]]
--local auth =  "eyJrZXkiOiJGSmRrNFdBQmY2WnByc0NFSG40ZzNUcmZZMDlBNk9HNXNcL0NiczNzblhzdTQ3NTczRDJ5dXFNN1wvdXFlRFwvMldCclZyU3hEbEdEb0owWUJtV1RpekV5OWlFMVdoNmpiZ0Y4dG5mWWwrY0hKSHBVTmtSOWx4QkxCQjVvOTN1WTZGMFwvRzEyODBxY09WQUtXazExVklqT1pQM3pRVzJzeUVMeDVvM200QzVYcitKQ3pxdmhqM2hjdWx1MXJCdjJkRUg0YTlGaWZtNmpERXUyU1NkajJqcWRYb0M2Y013eUZnb08rc1dqVUtNNUtqekZZN3pJc1IrVTZwM0R3MWZnQ2dSRVZCdUhpRFdyUGdCXC9HbXNLaVZoM1JGdzF6aWlwWEVJSnhtZ2VOeUFqNnlVSGp2K2lVVkVGTThzNkdVc05UMUN1ZmpHK0NGV3VyRjg4VTZSQlRKeGdhQT09IiwiZGF0YSI6IjN0TFhNVFRKRTJkdWZtUlFcL2tuTjlcL3Z4Qm8zOFhlT2t1WE1UUHVKSHMrYXRMdFc3YW96QzlFY3IwcFwvTVJGdlJFRDlcL3NaY3p6ellQRlI4aDJocmtyWjhjUzdSQ2F0ZDRLMHhNXC9zaGpXclNzQTd1NG5HaE5kdTMyeTQ1bm9TUXgiLCJtZDUiOiJQdHp0YXo5b1ZKalgzb29HcTRVczVuYWgyVVwvbkxKcXpFNkNHMWVXSnRPMFVpa2ZhNjFEa1RNZVNhZ0hSUlwvYlFtdThYWXlOQWY3RERhekJxSDZcL2ZIR0hObkhSYzhpeDVOWWo1Mmt1cERuK3VMb3BDUlM0Q0lKREdhbjZqd2VnRnUzQjdia0RQbERRYzRKRlpCNmpXc1B0emFkdGtWVUZvOWYxazFKNk1Qd3dGNUx2WHVsSDB1MTg1SVVCeHJyNnZBRXdhcVBxUTYxbUpVZFg5ZE5RWTR2UzVoalp1UU85bXpEd3d1UDhMblwvVVY1QnAxUmNVVmZqVFA4MThzTjRyTHhCZk9lMzBHR2pVYkR1SEhpUlwvVjhyVzZGN0RiYWpIYXBcL0lnbmJrN1wvYkJaU1NjTEF4dkNkajc2Y2EwSmlqNHJqOHZPRG0rcXFGeWVtMWJlcFV3VUhBPT0ifQ=="
--假装保存了 auth
local response_body = {}
http.TTIMEOUT = 8
local staus,code,header = http.request{
    --触动精灵端口号为 50005,小精灵端口号请修改为 50015
    url = "http://10.0.0.63:50005/deviceid",
    headers =
    {
        ["Content-Type"] = "application/x-www-form-urlencoded",
    },
    sink = ltn12.sink.table(response_body),  --用 ltn12 转化接受的数据为 lua 可用

}
dialog(table.concat(response_body))
--将会打印[[aab9d7903393a马赛克以下c0c0f51b595b70]]
local response_body = {}
local staus,code,header = http.request{
    url = "http://10.0.0.63:50005/snapshot?exp=png&orient=0",
    headers =
    {
        ["Content-Type"] = "application/x-www-form-urlencoded",
        --触动专业版 iOS 无需写入验证串 auth
        ["auth"] = auth,
    },
    sink = ltn12.sink.table(response_body),  --用 ltn12 转化接受的数据为 lua 可用

}
local function writePicture(picpath, content)
    local file = io.open(picpath,"wb")
    if file then
        file:write(content)
        file:close()
        return true
    end
    return false
end
writePicture("C:\\Users\\Administrator\\Desktop\\1.png",table.concat(response_body))
--在桌面写入一张图片
local response_body = {}
local staus,code,header = http.request{
    url = "http://10.0.0.63:50005/getFileList",
    headers =
    {
        ["Content-Type"] = "application/x-www-form-urlencoded",
        --触动专业版 iOS 无需写入验证串 auth
        ["auth"] = auth,
        ["Root"] = "lua",
        ["Path"] = "/"
    },
    sink = ltn12.sink.table(response_body),  --用 ltn12 转化接受的数据为 lua 可用    
}
print(table.concat(response_body))
--我的打印{"ret":true,"Dirs":[],"Path":"\/","Files":[".12786.cfg",".383.cfg",".8308.cfg","12786.tsp","1467787409.lua","383.tsp","8308.tsp","English.lua"]}

注意事项

  • 示例代码中的 key 可以在开发者平台获取。
  • 触动专业版 iOS 无需写入 key,无需写入验证串 auth。
Copyright 北京帮你玩科技有限公司 2024 all right reserved,powered by Gitbook该文章修订时间: 2024-10-08 11:38:05

results matching ""

    No results matching ""