2

I'm currently trying to login to LinkedIn, outside of their oAuth system (as it doesn't have the option for a task at hand). I'm not entirely familiar with cURL, so I may have some silly mistake somewhere.

However I don't seem to be having any luck what so ever, even after taking a look at similar questions on stackoverflow but for python. I'm getting the error

"Request Error We’re sorry, there was a problem with your request. Please make sure you have cookies enabled and try again."

I've fetched the loginCsrfParam and have checked out the postfield sent (it all checks out), I've tried sending the postfields as either an array or as a string like: session_key=".$username."&session_pass...

Here is my php code, any ideas?

<?php
class linkedInLogin {
    public function login(){
        $username = 'Email Here';
        $password = 'Password here';
        $csrf = $this->fetchCSRF();
        if($csrf == false){
            echo 'Error: loginCsrfParam fetching failed.';
            return;
        }
        //login form action url
        $url="https://www.linkedin.com/uas/login-submit";
        //$postinfo = "session_key=".$username."&session_password=".$password."&loginCsrfParam=".$csrf;
        $postfields = array('session_key' => $username, 'session_password' => $password, 'loginCsrfParam' => $csrf);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_COOKIESESSION, true);
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_USERAGENT,
            "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //@TODO change back: curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_REFERER, 'https://www.linkedin.com/uas/login');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
        echo curl_exec($ch);
        curl_close($ch);
    }

    // Fetches the CSRF for login authentication
    private function fetchCSRF(){
        $url = 'https://linkedin.com/uas/login';
        $html = file_get_contents($url);
        $precsrf = (int) strpos($html, '<input type="hidden" name="loginCsrfParam" value="');
        $postcsrf = (int) strpos($html, '" id="loginCsrfParam-login">');
        $length = $postcsrf - $precsrf - 50; // The -50 / + 50 is to correct for: <input type="hidden" name="login...
        $csrf = substr($html, $precsrf + 50, $length);
        if($csrf == false){
            return false;
        }
        return $csrf;
    }
}

Update: I still haven't had any luck with this, although other services don't offer similar functionality that I require.

Community
  • 1
  • 1
Adam
  • 146
  • 1
  • 15
  • On a vaguely related note, there's news that [millions of LinkedIn accounts are for sale](http://www.bbc.co.uk/news/technology-36320322) so they may well be making a lame, late attempt at curtailing data breaches. – Martin May 18 '16 at 17:29
  • Hi Adam, did you manage to solve this problem? I am facing exactly the same one now and I am wondering if you were successful with your attempt. Thanks! – Jacek Francuz Aug 18 '16 at 12:29
  • Hi Jacek, unfortunately I was not. Looking back at it now, I'm still not entirely sure how I'd go about doing it. Perhaps using a javascript framework instead of php would work. Not sure. – Adam Aug 20 '16 at 16:54

0 Answers0