It might sound silly; but can we programmatically login into a site such as Linkedin by passing our user credentials (userid and password)? I am not talking about using OAuth or other mechanism.
Edit:
I am using the following code in Page_Load method of an .aspx web page to login into Linkedin but it’s not working. I see the login page again:
try {
string url = "https://www.linkedin.com/uas/login";
string userName = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="24774b49416149454d4864774b4941604b49454d4a0a674b49">[email protected]</a>";
string password = "--------";
string domain = "linkedin.com";
string postData = "session_key=" + userName + "&session_password=" + password;
string userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";
string method = "POST";
string contentType = "text/html; charset=UTF-8";
HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest;
http.UserAgent = userAgent;
http.Method = method;
http.ContentType = contentType;
http.Credentials = new NetworkCredential(userName, password, domain);
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = dataBytes.Length;
http.Timeout = -1;
http.KeepAlive = true;
http.CookieContainer = new CookieContainer();
using (Stream postStream = http.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
if (httpResponse.Cookies != null)
{
foreach (Cookie cok in httpResponse.Cookies)
{
if (cok != null)
{
Response.Write(String.Format("Name = {0}- Value = {1}- Path = {2}<br/>", cok.Name, cok.Value, cok.Path));
}
}
}
Response.Write(httpResponse.Headers.ToString() + "<br/>");
Response.Write(new StreamReader(httpResponse.GetResponseStream()).ReadToEnd());
} catch(Exception ex)
{
Response.Write(ex.ToString());
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can login to many sites this way, using scripting. I generally prefer Python with BeautifulSoup, but there are many other possibilities for languages and libraries.
The general idea is to locate the code for the form that has the username and password fields, fill them in, then post the form.
This works for me on Linux, with my LinkedIn username and password in my .netrc in the standard format:
#!/usr/bin/python
import sys, os, urllib, urllib2, cookielib, urlparse, netrc
from BeautifulSoup import BeautifulSoup
URL = 'https://www.linkedin.com/uas/login'
COOKIES = urllib2.HTTPCookieProcessor(cookielib.CookieJar())
HANDLER = getattr(urllib2, urlparse.urlsplit(URL).scheme.upper() + 'Handler')
DEBUGLEVEL = 0 # set to 1 to see unencrypted data
OPENER = urllib2.build_opener(COOKIES, HANDLER(debuglevel = DEBUGLEVEL))
def fetch(url, data = None):
connection = OPENER.open(url, data)
page = connection.read()
connection.close()
return page
def login():
soup = BeautifulSoup(fetch(URL))
form = soup.find('form')
fields = form.findAll('input')
auth_info = netrc.netrc().authenticators(urlparse.urlsplit(URL).netloc)
formdata = dict([[field['name'], field['value']] for field in fields])
formdata['session_key'], ignored, formdata['session_password'] = auth_info
assert form['method'] == 'POST'
posturl = urlparse.urljoin(URL, form['action'])
print fetch(posturl, urllib.urlencode(formdata))
if __name__ == '__main__':
login()
Method 2
Yes, you could use a HttpWebRequest in order to send HTTP requests to a given URL. So the idea would be to POST your credentials to the given url which is supposed to authenticate the user and capture any cookies that could be used on subsequent requests to authenticated parts of the site.
Of course that’s pretty raw work, if the remote site provides an API you might also use this API as it will simplify things.
Method 3
You can use tools like selenium to automate and generate code for programmatically login. Also a search in google sure reveals more tools aimed to do “mashups” of web sites, like BeautifulSoup recommended in first answer.
Regards
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0