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

时间:2018-06-29 06:52:41

标签: mediawiki-api

所以,我要尝试的是使用机器人帐户登录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

由于我深陷其中,因此不胜感激!

1 个答案:

答案 0 :(得分:0)

当响应给您一个令牌时,表示您没有发送令牌,因此登录失败,并且该API假设您使用的是已弃用的“首先尝试不使用令牌的登录尝试,请获取一个带有令牌的错误消息,请使用令牌”协议重复登录。

如果您正在发送令牌,则您的会话(验证令牌的对象)将丢失。通常,这是由客户端中不正确(或没有)的cookie处理引起的。