Итак, я пытаюсь войти в экземпляр mediawiki с учетной записью бота.
Согласно API, первым шагом для входа в систему является получение токена для входа.
URL anUrl;
String s;
try {
anUrl = new URL("https://home.blazingumbra.com/wiki/api.php?action=query&format=json&meta=tokens&type=login");
HttpURLConnection connection = (HttpURLConnection) anUrl.openConnection();
ReaderMaker maker = new ReaderMaker();
BufferedReader br = maker.fromInputStream(connection.getInputStream());
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
return;
}
//convert it into pojo.
Gson gson = new Gson();
QueryLoginTokenResponse queryLoginTokenResponse = gson.fromJson(s,
QueryLoginTokenResponse.class);
С этим токеном я пытаюсь наконец войти в систему:
(API требует отправки POST-запроса)
try {
String loginToken = queryLoginTokenResponse.getToken();
URL anURL = ECommands.makeURL("https://home.blazingumbra.com/wiki/api.php?action=login&format=json");
HttpURLConnection connection = (HttpURLConnection) anURL.openConnection();
connection.setUseCaches(true);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "text");
connection.connect();
String body = "&lgname=bot&lgpassword=secret&lgtoken="+loginToken;
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(body);
int responseCode = connection.getResponseCode();
ReaderMaker readerMaker = new ReaderMaker();
BufferedReader in = readerMaker.fromInputStream(connection.getInputStream());
StringBuilder stringBuffer = new StringBuilder();
while((s =in.readLine()) != null) {
stringBuffer.append(s);
}
s = stringBuffer.toString();
} catch (IOException|NullPointerException e) {
msg.editMessage("There's a mistake while trying to call the login page.").complete();
return;
}
Проблема в том, что после попытки входа в систему ответ выглядит следующим образом:
{"warnings":{"login":{"*":"Fetching a token via action=login is deprecated. Use action=query&meta=tokens&type=login instead."}},"login":{"result":"NeedToken","token":"b116120f436071e5209a9b0e707e8c045b35d61e+\\"}}
Вход-API-страницу
Так как я застрял здесь в глубине, любая помощь будет принята с благодарностью!