yclown / ql_jd_cookie

青龙面板京东cookie获取桌面版,支持一键发送到青龙面板,账号密码快速填入
278 stars 46 forks source link

半吊桶小白求助 #32

Closed liuckv closed 2 days ago

liuckv commented 1 month ago

自己弄了代码,但是用青龙上传的时候,提示token失败,麻烦老大帮忙看看改进改进。

-- 导入 Lua 和 Android 必要的模块 require "import" import "android.app." import "android.os." import "android.widget." import "android.view." import "layout" import "android.webkit.CookieSyncManager" import "android.webkit.CookieManager" import "android.content.Context" import "android.content.Intent" import "android.net.Uri" import "android.view.View"

-- 使用 luajava 绑定 Java 类 URL = luajava.bindClass("java.net.URL") OutputStreamWriter = luajava.bindClass("java.io.OutputStreamWriter") BufferedReader = luajava.bindClass("java.io.BufferedReader") InputStreamReader = luajava.bindClass("java.io.InputStreamReader") StringBuilder = luajava.bindClass("java.lang.StringBuilder")

-- 设置主题和布局 activity.setTheme(R.Theme_Blue) activity.setTitle("饿了么Cookie") activity.setContentView(loadlayout(layout))

-- 登录链接 loginurl = "饿了么地址" web.loadUrl(loginurl)

-- 设置网页 Cookie 函数 function setCookie(context, url, content) CookieSyncManager.createInstance(context) local cookieManager = CookieManager.getInstance() cookieManager.setAcceptCookie(true) cookieManager.setCookie(url, content) CookieSyncManager.getInstance().sync() end

-- 获取网页 Cookie 函数 function getCookie(url) local cookieManager = CookieManager.getInstance() return cookieManager.getCookie(url) end

-- 删除网页 Cookie 函数 function delCookie() local cookieManager = CookieManager.getInstance() cookieManager.removeSessionCookie() cookieManager.removeAllCookie() end

-- 获取青龙面板 Token 的函数 function getQinglongToken(client_id, client_secret) local url = URL("http://青龙地址/open/auth/token") local conn local response local jsonResponse

pcall(function() conn = url:openConnection() conn:setRequestMethod("POST") conn:setRequestProperty("Content-Type", "application/json") conn:setDoOutput(true)

-- 创建 JSON 数据
local jsonData = '{"client_id": "' .. client_id .. '", "client_secret": "' .. client_secret .. '"}'

-- 发送数据
local out = OutputStreamWriter(conn:getOutputStream())
out:write(jsonData)
out:flush()
out:close()

-- 读取响应数据
local responseCode = conn:getResponseCode()
if responseCode == 200 then
  local input = BufferedReader(InputStreamReader(conn:getInputStream()))
  response = StringBuilder()
  local line
  while true do
    line = input:readLine()
    if line == nil then break end
    response:append(line)
  end
  input:close()

  -- 将响应 JSON 解析为 Lua 表
  jsonResponse = require "cjson".decode(response:toString())
else
  print("获取 Token 失败, 错误码:", responseCode)
end

end)

return jsonResponse and jsonResponse.data.token or nil end

-- 上传 Cookie 到青龙面板的函数 function uploadToQinglong(cookie, ql_url, ql_token) if cookie == "" then print("未获取到Cookie,无法上传") return end

local url = URL(ql_url) local conn local response

pcall(function() conn = url:openConnection() conn:setRequestMethod("POST") conn:setRequestProperty("Content-Type", "application/json") conn:setRequestProperty("Authorization", "Bearer " .. ql_token) conn:setDoOutput(true)

-- 创建 JSON 数据
local jsonData = '{"name": "elmck", "value": "' .. cookie .. '", "remarks": "饿了么Cookie"}'

-- 发送数据
local out = OutputStreamWriter(conn:getOutputStream())
out:write(jsonData)
out:flush()
out:close()

-- 获取响应码
local responseCode = conn:getResponseCode()
if responseCode == 200 then
  print("Cookie上传成功")
else
  print("Cookie上传失败, 错误码:", responseCode)
end

end) end

-- 网页状态监听 web.setWebViewClient{ shouldOverrideUrlLoading = function(view, url) -- 即将跳转 URL end,

onPageStarted = function(view, url, favicon) -- 网页开始加载 end,

onPageFinished = function(view, url) -- 网页加载完成,获取当前网页的 Cookie webcookie = getCookie(url)

if webcookie ~= nil and webcookie:find("SID=") then
  -- 获取到 Cookie 后展示
  cookie = webcookie
  mycookietxt.setText(cookie)
  cookielay.setVisibility(View.VISIBLE)
end

end }

-- 复制 Cookie 按钮 cklaymyck.onClick = function() import "android.content.Context" activity.getSystemService(Context.CLIPBOARD_SERVICE).setText(cookie) print("复制成功") end

-- 重新获取 Cookie 按钮 cklaycxhqck.onClick = function() cookielay.setVisibility(View.GONE) -- 隐藏控件 delCookie() cookie = "" os.execute("rm -rf /data/data/" .. this.packageName .. "/cache/") os.execute("rm -rf /data/data/" .. this.packageName .. "/files/data/") web.loadUrl(loginurl) -- 重新加载网页 end

-- 上传 Cookie 按钮 cklayupload.onClick = function() if cookie == "" or cookie == nil then print("Cookie为空,无法上传") return end

-- 使用你的 Client ID 和 Client Secret local client_id = "fk5NI3MBR_e4" local client_secret = "_XRPTt1KGkbaOIh518Bp59cm"

-- 获取 Token local ql_token = getQinglongToken(client_id, client_secret)

if ql_token == nil then print("Token 获取失败,无法上传 Cookie") return end

-- 青龙面板的 URL local ql_url = "http://青龙地址/open/auth/token?client_id=fk5NI3MBR_e4&client_secret=_XRPTt1KGkbaOIh518Bp59cm"

-- 上传 Cookie uploadToQinglong(cookie, ql_url, ql_token) end

yclown commented 1 month ago

你调试过获取token可以了不 image 这里登录地址直接用字符拼接方式写在url里,后面的请求方式改成get

liuckv commented 1 month ago

调试过拼接地址可以获取TOKEN,之前复制错了。 URL里面的登录地址填了拼接的登录地址,还是提示获取token失败 11222

yclown commented 1 month ago

那下面的 image 附加数据应该不需要了,获取不到应该是你lua进行get请求的方式不是这样,要换一种

liuckv commented 1 month ago

注释了红框部分,还是提示获取token失败。 求再赐教!

yclown commented 1 month ago

我对lua也不熟,你发个gpt看看,就使用lua进行get请求

liuckv commented 1 month ago

我再试试看,不行再请教