获取登录令牌,即使我已经取回了一个

Getting a login token even though I've already retrieved one

所以,我想做的是使用机器人帐户登录到 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+\"}}

Login-API-Page

由于我有点被困在这里,任何帮助将不胜感激!

当响应给你一个令牌时,这意味着你没有发送一个令牌,因此登录失败,并且 API 假设你正在使用长期弃用的 "do a login attempt without a token first, get an error message with a token, repeat login with token" 协议.

如果您正在发送令牌,那么您的会话(验证令牌的依据)就会丢失。这通常是由于客户端中的 cookie 处理不正确(或没有)造成的。