views:

264

answers:

1

Hi folks, this week I'm having problems logging in LinkedIn using ruby mechanize. My code is as follows:

agent = WWW::Mechanize.new
home_page = agent.get('http://www.linkedin.com')
sign_in_link = home_page.links.find{|link| link.text == "Sign In"}
login_form = sign_in_link.click.form('login')
# with email and password variables properly set
login_form.set_fields(:session_key => email, :session_password => password)
return_page = agent.submit(login_form, login_form.buttons.first)

Last week it worked OK but now it's failing, the return_page variable shows a 'redirecting...' message but when I use the same agent to fetch 'http://www.linkedin.com/home', it's as if I've never signed in (although cookies show otherwise). Can someone try to duplicate this error?

Thanks in advance.

+1  A: 

In our case we succeded using curl inside our controller, here is a snippet of what we do, we do a curl call to log in and store the cookies in a file named j and then use it for every subsequent request, hacky but works in the meantime.

 curl_login = `curl --cookie-jar j -so/dev/null -Fsession_login= -Fsession_rikey= -Fsession_key=#{contact} -Fsession_password=#{password} https://www.linkedin.com/secure/login?trk=hb_signin`

 curl_for_profile = `curl --cookie j "#{profile_url}"`

Let me know if you have any question

MexicanHacker
Hi Oscar, thanks! I'm gonna work a little more on mechanize but your solution will help me in the meantime ;)
thiagobrandam
Well, I managed to alter your solution to use with mechanize , I just add your 'j' file to the cookie jar (agent.cookie_jar.load('j',:cookiestxt))
thiagobrandam
Cool, we'll also give it a try.
MexicanHacker