5

I need to login into Linkedin with Jsoup, preferably.

This is what I'm using to login to another website but it isn't working for Linkedin.

Connection.Response res = Jsoup
    .connect("https://www.linkedin.com/uas/login?goback=&trk=hb_signin")
    .data("session_key", mail, "session_password", password)
    .method(Connection.Method.POST)
    .timeout(60000).

// Also tried "https://www.linkedin.com/uas/login-submit"

Map<String, String> loginCookies = res.cookies();
    //Checking a profile to see if it was succesful or if it returns the login page.    
Document currentPage = Jsoup.connect(someProfileLink).cookies(loginCookies).timeout(10000).
System.out.println("" + currentPage.text());

What am I doing wrong?

I need to be able to fetch user profiles by using a web crawler but whatever I try I can't get the login cookies.

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
Sorin Grecu
  • 1,036
  • 11
  • 30

1 Answers1

3

You can login into Linkedin with this code:

    try {

                String url = "https://www.linkedin.com/uas/login?goback=&trk=hb_signin";
                Connection.Response response = Jsoup
                        .connect(url)
                        .method(Connection.Method.GET)
                        .execute();

                Document responseDocument = response.parse();
                Element loginCsrfParam = responseDocument
                        .select("input[name=loginCsrfParam]")
                        .first();

                response = Jsoup.connect("https://www.linkedin.com/uas/login-submit")
                        .cookies(response.cookies())
                        .data("loginCsrfParam", loginCsrfParam.attr("value"))
                        .data("session_key", "your_login")
                        .data("session_password", "your_password")
                        .method(Connection.Method.POST)
                        .followRedirects(true)
                        .execute();

                Document document = response.parse();

    //            System.out.println(document)

                System.out.println("Welcome " 
                        + document.select(".act-set-name-split-link").html());

            } catch (IOException e) {
                e.printStackTrace();
            }
Sestertius
  • 1,367
  • 1
  • 14
  • 13
  • 1
    Thanks a bunch! It works, of course. A last request, if I may: could you please explain the process? Why does it need the response to login? What is Linkedin using for JSoup to need to do things this way and not the simple way? – Sorin Grecu Jul 08 '15 at 07:04
  • 2
    @SorinGrecu To pass the authentication process you must add session cookies and CSRF token to your POST request. Read [here](http://stackoverflow.com/questions/5207160/what-is-a-csrf-token-what-is-its-importance-and-how-does-it-work) about CSRF tokens. – Sestertius Jul 08 '15 at 08:04
  • Does not seem to work any more. Any ideas as to why ? – DevilCode Jun 10 '18 at 02:53