HtmlUnit实现Linkedin网站登录认证

importorg.scribe.builder.ServiceBuilder;

importorg.scribe.builder.api.LinkedInApi;

importorg.scribe.model.Token;

importorg.scribe.oauth.OAuthService;

importcom.gargoylesoftware.htmlunit.BrowserVersion;

importcom.gargoylesoftware.htmlunit.HttpWebConnection;

importcom.gargoylesoftware.htmlunit.WebClient;

importcom.gargoylesoftware.htmlunit.html.HtmlPage;

importcom.gargoylesoftware.htmlunit.html.HtmlPasswordInput;

importcom.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

importcom.gargoylesoftware.htmlunit.html.HtmlTextInput;

publicclassLinkedInExample{

privatestaticfinalStringAUTHORIZE_URL="https://api.linkedin.com/uas/oauth/authorize?oauth_token=";

//privatestaticStringPROTECTED_RESOURCE_URL="http://api.linkedin.com/v1/people/~/connections:(id,last-name)";

privatestaticStringPROTECTED_RESOURCE_URL="http://api.linkedin.com/v1/people/~/network?type=SHAR&scope=self";

publicstaticvoidmain(String[]args)throwsException

{

//IqTzOCsQ1U_XCXfeTVPTDjwwSq6qvtQNwMVLgy28KlPGNptWdyfFu-zXj8cABDAL

Stringappkey="";//APIKey

//QIy3WwQs7x_S_vHgwjsY0YmBhgbDlQpGb4tB2l_jwAdq9hiHUx-7kD_-lcUZmCgs

Stringappsecret="";//APISecret

OAuthServiceservice=newServiceBuilder()

.provider(LinkedInApi.class)

.apiKey(appkey)

.apiSecret(appsecret)

.build();

//ObtaintheRequestToken

System.out.println("FetchingtheRequestToken...");

TokenrequestToken=service.getRequestToken();

StringrequestURL=AUTHORIZE_URL+requestToken.getToken();

System.out.println(requestURL);

finalWebClientwebClient=newWebClient(BrowserVersion.FIREFOX_3);

webClient.setJavaScriptEnabled(true);

webClient.setActiveXNative(true);

webClient.setCssEnabled(true);

webClient.setAppletEnabled(true);

HttpWebConnectionhttpwebconnection=newHttpWebConnection(webClient);

httpwebconnection.setUseInsecureSSL(true);

webClient.setWebConnection(httpwebconnection);

finalHtmlPagepage=webClient.getPage(requestURL);

//System.err.println(page.asXml());

HtmlTextInputkey=(HtmlTextInput)page.getElementById("session_key-oauthAuthorizeForm");

key.setValueAttribute("");//linkedin用户名

System.err.println(key.asXml());

HtmlPasswordInputpassword=(HtmlPasswordInput)page.getElementById("session_password-oauthAuthorizeForm");

password.setValueAttribute("");//linkedin密码

System.err.println(password.asXml());

HtmlSubmitInputsubmit=(HtmlSubmitInput)(page.getElementsByName("authorize").get(0));

System.err.println(submit.asXml());

HtmlPagepage2=submit.click();

System.out.println(page2.asXml());

webClient.closeAllWindows();

}

}

-------------------------------------------------------------

1。需要注册才可以。

//APIKey

//APISecret

//linkedin用户名

//linkedin密码

2。不知道是什么地方出问题,总是登录不成功,不知道什么原因?请高手指导下,谢了!

出错提示为:

We’resorry,therewasaproblemwithyourrequest.Pleasemakesureyouhavecookiesenabledandtryagain.

相关推荐