Unofficial LinkedIn API in .NET (C#)
The LinkedIn API has of late been very much limited to very basic functionality, and you may want to get more information out of it.
I based this API on these two sources
// https://github.com/nickls/linkedin-unofficial-api
and
// https://www.tarlogic.com/blog/usuarios-y-direcciones-de-correo-con-linkedin-harverster/
Unfortunately, this code is actually not suitable as a web service or from a website, since LinkedIn will detect that the login request is coming from a unusual IP (location), and kick you out. But it would be useful for client-side code.
So, here’s the login code – at a high level
HttpOverTcpRequest http = new HttpOverTcpRequest();
var welcomeRaw = http.Request(“https://www.linkedin.com/uas/authenticate”);
var welcome = http.ParseHttpResponse(welcomeRaw);
var lCookies = welcome.Headers[“Set-Cookie”];
lCookies.RemoveAll(c => c.Contains(“delete me”) || c.Contains(“deleteMe”)); // Remove empty cookies
var jSessionIdRaw = lCookies.First(c => c.Contains(“JSESSIONID”));
var jSessionId = jSessionIdRaw.Split(new[] { ‘;’ })[0].Substring(12);
var postdata = “session_key=” + username + “&session_password=” + password + “&JSESSIONID=”+ jSessionId;
var loginRaw = http.Request(“https://www.linkedin.com/uas/authenticate”, postdata, lCookies);
var login = http.ParseHttpResponse(loginRaw);
var lCookies2 = login.Headers[“Set-Cookie”];
lCookies.AddRange(lCookies2);
Then, you need to keep hold of your lCookies array, and pass it back in when making requests such as;
// /li/v1/pages/you
var YouRaw = http.Request(“https://touch.www.linkedin.com/” + query, lCookiesFromDb);
var you = http.ParseHttpResponse(YouRaw);
var strJson = RemoveQuirks(you.Body);
Response.Write(strJson);
Looks like this approach is no longer working, I’m getting this error;
loginRaw,nq
HTTP/1.1 403 Forbidden
Server: Apache-Coyote/1.1
P3P: CP=”CAO CUR ADM DEV PSA PSD OUR”
Content-Length: 0
Vary: Accept-Encoding
Date: Mon, 16 Jan 2017 16:55:23 GMT
X-FS-UUID: bf442093d94e9a14c0cf1d61f52a0000
Content-Security-Policy-Report-Only: default-src *; connect-src ‘self’ static.licdn.com s.c.lnkd.licdn.com static-fstl.licdn.com static-src.linkedin.com; img-src data: blob: *; font-src data: *; style-src ‘unsafe-inline’ ‘self’ static-src.linkedin.com *.licdn.com; script-src ‘unsafe-inline’ ‘unsafe-eval’ ‘self’ platform.linkedin.com spdy.linkedin.com static-src.linkedin.com *.ads.linkedin.com *.licdn.com ssl.google-analytics.com bcvipva02.rightnowtech.com sjs.bizographics.com js.bizographics.com static.chartbeat.com; object-src static.licdn.com http://www.youtube.com; frame-ancestors ‘self’; report-uri https://www.linkedin.com/lite/contentsecurity?f=l
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Li-Fabric: prod-ltx1
Strict-Transport-Security: max-age=0
Set-Cookie: _lipt=deleteMe; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store
X-Li-Pop: PROD-IDB2
X-LI-UUID: v0Qgk9lOmhTAzx1h9SoAAA==
string
LikeLike
This code now works as an alternative:
var strUrl = “https://www.linkedin.com/”;
var strHtml = Http.Request(strUrl);
const string strNvRegex = @”(?\w+)-login..{22}(?[\w-]+)”;
var mcNv = Regex.Matches(strHtml, strNvRegex);
var dNv = mcNv.Cast().ToDictionary(d => d.Groups[“Name”].Value, d => d.Groups[“Value”].Value);
var strPostdata = “session_key=xxxxxxx”;
strPostdata += “&session_password=xxxxx”;
strPostdata += “&isJsEnabled=false”;
strPostdata += “&loginCsrfParam=” + dNv[“loginCsrfParam”];
strPostdata += “&sourceAlias=” + dNv[“sourceAlias”];
strUrl = “https://www.linkedin.com/uas/login-submit”;
var strHtml2 = Http.Request(strUrl,”POST”, strPostdata);
LikeLike