修复B站无法登陆问题

This commit is contained in:
Yutou 2024-01-08 17:31:21 +08:00
parent 177c91c370
commit 3437a5386f
2 changed files with 86 additions and 41 deletions

View File

@ -71,6 +71,54 @@ public class BiliBiliUtils {
return null;
}
public JSONObject http_login_getSid(String url) {
try {
Log.i("调用url = "+url);
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
setConnection(null,connection);
connection.setRequestMethod("GET");
connection.connect();
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = stream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
outputStream.flush();
}
List<String> cookie = null;
if (connection.getHeaderFields().containsKey("Set-Cookie")) {
cookie = connection.getHeaderFields().get("Set-Cookie");
}
String str = outputStream.toString(StandardCharsets.UTF_8);
outputStream.close();
try {
JSONObject json = JSON.parseObject(str);
if(cookie!=null){
StringBuilder ck=new StringBuilder();
for (String key : cookie) {
ck.append(";").append(key);
}
json.put("cookie",ck.toString());
}
return json;
} catch (Exception e) {
JSONObject json = new JSONObject();
json.put("html", str);
return json;
} finally {
stream.close();
connection.disconnect();
}
} catch (IOException e) {
//com.yutou.bilibili.Tools.Log.e(e);
e.printStackTrace();
}
return null;
}
public JSONObject http_post(String url, String body) {
return http(url, HTTP.POST, body, RET_MODEL.JSON);
}
@ -318,7 +366,9 @@ public class BiliBiliUtils {
connection.setRequestProperty("Referer", "https://www.bilibili.com");
connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("Upgrade-Insecure-Requests", "1");
connection.addRequestProperty("Cookie", cookie);
if(!StringUtils.isEmpty(cookie)) {
connection.addRequestProperty("Cookie", cookie);
}
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36");
}

View File

@ -4,16 +4,14 @@ import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.utlis.ConfigTools;
import com.yutou.qqbot.utlis.HttpTools;
import com.yutou.qqbot.utlis.QRCodeUtils;
import com.yutou.qqbot.utlis.StringUtils;
import com.yutou.qqbot.utlis.*;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
public class BiliLogin {
@ -27,11 +25,12 @@ public class BiliLogin {
}
public JSONObject login() {
JSONObject login = JSON.parseObject(HttpTools.get("https://passport.bilibili.com/qrcode/getLoginUrl"));
JSONObject login = JSON.parseObject(HttpTools.get("https://passport.bilibili.com/x/passport-login/web/qrcode/generate"));
JSONObject json = new JSONObject();
System.out.println("login = " + login);
json.put("code", login.getInteger("code"));
json.put("url", login.getJSONObject("data").getString("url"));
new Thread(() -> waitLogin(login.getJSONObject("data").getString("oauthKey"))).start();
new Thread(() -> waitLogin(login.getJSONObject("data").getString("qrcode_key"))).start();
return json;
}
@ -43,47 +42,39 @@ public class BiliLogin {
public void waitLogin(String oauthKey) {
long time = System.currentTimeMillis();
String bd = "gourl=https%3A%2F%2Fpassport.bilibili.com%2Fajax%2FminiLogin%2Fredirect&oauthKey=" + oauthKey;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
String bd = "gourl=https%3A%2F%2Fpassport.bilibili.com%2Fajax%2FminiLogin%2Fredirect&oauthKey=" + oauthKey;
if ((System.currentTimeMillis() - time) > 5 * 60 * 1000) {
cancel();
return;
}
JSONObject json = JSON.parseObject(HttpTools.post("https://passport.bilibili.com/qrcode/getLoginInfo", bd.getBytes(StandardCharsets.UTF_8)));
if (json.containsKey("code") && json.getInteger("code") == 0) {
System.out.println("json = " + json);
String _url = json.getJSONObject("data").getString("url");
Map<String, String> map = HttpTools.getUrlParams(_url);
JSONObject json = biliUtils.http_login_getSid("https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=" + oauthKey);
Log.i("B站登陆",json.toJSONString());
if (json.getInteger("code") == 0 && json.getJSONObject("data").getInteger("code") == 0) {
JSONObject cookie = new JSONObject();
JSONArray array = new JSONArray();
for (String key : map.keySet()) {
cookie.put(key, map.get(key));
String[] split = json.getString("cookie").split(";");
JSONObject ck = new JSONObject();
for (String string : split) {
if (!ck.containsKey(string) && !StringUtils.isEmpty(string) && string.contains("=")) {
String key = string.split("=")[0].trim();
String value = string.split("=")[1].trim();
if (key.contains("Expires")) {
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMMM yyyy HH:mm:ss z", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getDefault());
Date date = sdf.parse(value, new ParsePosition(0));
value = String.valueOf(date.getTime() / 1000);
}
ck.put(key, value);
}
}
System.out.println(array);
if (biliUtils == null) {
biliUtils = BiliBiliUtils.getInstance(qq);
}
assert biliUtils != null;
JSONObject tmp = biliUtils.http_post(_url, "");
System.out.println("tmp = " + tmp);
if (tmp == null) {
cancel();
return;
}
String sid = tmp.getString("cookie");
sid = sid.split("sid=")[1];
sid = sid.split(";")[0];
cookie.put("sid", sid);
cookie.put("Domain", ".bilibili.com");
ConfigTools.saveFile(new File(qq + "_bilibili.cookie"), cookie.toJSONString());
ck.put("gourl", bd);
ConfigTools.saveFile(new File(qq + "_bilibili.cookie"), ck.toJSONString());
cancel();
}
}
}, 0, 3000);
}, 1000, 3000);
}
@ -104,11 +95,15 @@ public class BiliLogin {
}
public static void main(String[] args) {
public static void main(String[] args) throws ParseException {
String http = "{\"code\":0,\"message\":\"0\",\"ttl\":1,\"data\":{\"url\":\"https://passport.biligame.com/x/passport-login/web/crossDomain?DedeUserID=96300&DedeUserID__ckMd5=c506a12068157a3a&Expires=1720252437&SESSDATA=c383a8e0,1720252437,9ba5c*11CjCBp3_j-6zYtK27tzUVrmFZNgjyeDW6pcdfmRBJU5cVfz4OhVmujjKCZsGAkXL6ll8SVnplZ2JONlBleHdFOGgzUzNJYUxIbzJwRVhTYVM2LWVuQnJyQVQxQnRWTUFZbUYybV95Y1RELTdGeF9mNlpXV3RDTjdsMVBaYkhxZGZyblRkTUJCMGV3IIEC&bili_jct=e786885293c24eb7198dc0538b56d6d8&gourl=https%3A%2F%2Fwww.bilibili.com\",\"refresh_token\":\"347b83e7c4a1e24f4681f06f28f71b11\",\"timestamp\":1704700437058,\"code\":0,\"message\":\"\"},\"cookie\":\";SESSDATA=c383a8e0%2C1720252437%2C9ba5c%2A11CjCBp3_j-6zYtK27tzUVrmFZNgjyeDW6pcdfmRBJU5cVfz4OhVmujjKCZsGAkXL6ll8SVnplZ2JONlBleHdFOGgzUzNJYUxIbzJwRVhTYVM2LWVuQnJyQVQxQnRWTUFZbUYybV95Y1RELTdGeF9mNlpXV3RDTjdsMVBaYkhxZGZyblRkTUJCMGV3IIEC; Path=/; Domain=bilibili.com; Expires=Sat, 06 Jul 2024 07:53:57 GMT; HttpOnly; Secure;bili_jct=e786885293c24eb7198dc0538b56d6d8; Path=/; Domain=bilibili.com; Expires=Sat, 06 Jul 2024 07:53:57 GMT;DedeUserID=96300; Path=/; Domain=bilibili.com; Expires=Sat, 06 Jul 2024 07:53:57 GMT;DedeUserID__ckMd5=c506a12068157a3a; Path=/; Domain=bilibili.com; Expires=Sat, 06 Jul 2024 07:53:57 GMT;sid=ger7871m; Path=/; Domain=bilibili.com; Expires=Sat, 06 Jul 2024 07:53:57 GMT\"}";
JSONObject body = JSON.parseObject(http);
BiliLogin login = new BiliLogin(583819556L);
boolean testLogin = login.testLogin();
System.out.println("testLogin = " + testLogin);
// JSONObject json = login.login();
System.out.println(BiliBiliUtils.getInstance(583819556L).getLoginInfo());
//JSONObject json = login.login();
//System.out.println("json = " + json);
// QRCodeUtils.createQRCode("bili_login", json.getString("url"));
}
}