LinkedIn uses oauth to enter its api. Unable to enter api on server. I tried to use an http request to enter linkedin and get oauth_verifier, but I got a response like this
Sorry, there was a problem with your request. Please make sure you enable cookies and try again.
Or follow this link to return to the home page.
I have analyzed the connection between my browser and server many times, but could not find why
public boolean Login(String user, String pass, String url) {
try {
DefaultHttpClient httpclient;
HttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, 100);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
httpclient = new DefaultHttpClient(cm, params);
String loginHTML = httpSession.Get(url);
for (Cookie c : httpSession.cs.getCookies()) {
httpclient.getCookieStore().addCookie(c);
}
Document doc = Session.GetDocument(loginHTML, url);
HashMap<String, String> hm = new HashMap<String, String>();
String postURL = doc.getElementsByAttributeValue("name", "oauthAuthorizeForm").get(0).absUrl("action");
HttpResponse response;
HttpEntity entity;
HttpPost httpost = new HttpPost(postURL);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
hm.put("session_login", user);
hm.put("session_password", pass);
hm.put("duration", "0");
hm.put("authorize", "Ok, I'll Allow It");
hm.put("extra", doc.getElementsByAttributeValue("name", "extra").get(0).attr("value"));
hm.put("access", doc.getElementsByAttributeValue("name", "access").get(0).attr("value"));
hm.put("agree", "true");
hm.put("oauth_token", doc.getElementsByAttributeValue("name", "oauth_token").get(0).attr("value"));
hm.put("appId", doc.getElementsByAttributeValue("name", "appId").get(0).attr("value"));
hm.put("csrfToken", doc.getElementsByAttributeValue("name", "csrfToken").get(0).attr("value"));
hm.put("sourceAlias", doc.getElementsByAttributeValue("name", "sourceAlias").get(0).attr("value") + "session_login=" + user);
for (Entry<String, String> i : hm.entrySet()) {
nvps.add(new BasicNameValuePair(i.getKey(), new String(i.getValue().getBytes(), "utf-8")));
}
hm.put("sourceAlias", doc.getElementsByAttributeValue("name", "sourceAlias").get(0).attr("value"));
for (Entry<String, String> i : hm.entrySet()) {
nvps.add(new BasicNameValuePair(i.getKey(), new String(i.getValue().getBytes(), "utf-8")));
}
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpost.setHeader("User-Agent", "Shisoft NetFusion");
response = httpclient.execute(httpost);
entity = response.getEntity();
Header headers[] = response.getHeaders("location");
for (Header h : headers) {
if (!h.getValue().isEmpty()) {
String newurl = h.getValue();
String oauthVerifier = newurl.split("oauth_verifier=")[1].split("&")[0];
accessToken = oauthService.getOAuthAccessToken(requestToken, oauthVerifier);
return true;
}
}
if (entity != null) {
String resHTML = EntityUtils.toString(entity);
httpost.abort();
httpclient.getConnectionManager().closeExpiredConnections();
}
httpost.abort();
return false;
} catch (Exception ex) {
Logger.getLogger(ClassLinkedIn.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
URL AuthorizationUr httpSession.Get (URL); gets the login page and cookies.