Log into Github with JSOUP

Viewed 144

My code looks like this at the moment (outdated example code?) and I would want to log into GitHub using JSOUP. But I am getting an error when I try to run this. I marked where I get an error below and I think the code is not using the right cssquery because its outdated.

     try {
        // # Constants used in this example
        final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 
       (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
        final String LOGIN_FORM_URL = "https://github.com/login";
        final String LOGIN_ACTION_URL = "https://github.com/session";
        final String USERNAME = "user";
        final String PASSWORD = "pass";

        // # Go to login page and grab cookies sent by server
        Connection.Response loginForm = Jsoup.connect(LOGIN_FORM_URL)
                .method(Connection.Method.GET)
                .userAgent(USER_AGENT)
                .execute();
        Document loginDoc = loginForm.parse(); // this is the document containing response html
        HashMap<String, String> cookies = new HashMap<>(loginForm.cookies()); // save the cookies to be passed on to next request

        // # Prepare login credentials
        **// This is where I get an error.**
        String authToken = loginDoc.select("#login > form > div:nth-child(1) > input[type=\"hidden\"]:nth-child(2)")
                .first()
                .attr("value");

        HashMap<String, String> formData = new HashMap<>();
        formData.put("commit", "Sign in");
        formData.put("utf8", "e2 9c 93");
        formData.put("login", USERNAME);
        formData.put("password", PASSWORD);
        formData.put("authenticity_token", authToken);

        // # Now send the form for login
        Connection.Response homePage = Jsoup.connect(LOGIN_ACTION_URL)
                .cookies(cookies)
                .data(formData)
                .method(Connection.Method.POST)
                .userAgent(USER_AGENT)
                .execute();

        System.out.println(homePage.parse().html());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
1 Answers

You're getting that error because the select is looking in the wrong place. (It's looking inside a div element but field you want is not in there anymore.) The good news is that the select for the auth token can be done a lot more easily now:

String authToken = loginDoc.selectFirst("input[name=authenticity_token]") != null ? loginDoc.selectFirst("input[name=authenticity_token]").val() : null;
           

The bad news is that the form now needs several other bits of data as well, and you'll need to do a series of similar select statements to fetch them all and and formData.put to pass them to the post.

  • trusted_device:
  • webauthn-support:
  • webauthn-iuvpaa-support:
  • return_to:
  • allow_signup:
  • client_id:
  • integration:
  • required_field_392c:
  • timestamp:
  • timestamp_secret:

As suggested in the comments, using the API is probably the best way to "future-proof" the app to avoid this sort of unpleasantness going forward.

Related