HtmlUnit实现Linkedin网站登录认证

import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.LinkedInApi;
import org.scribe.model.Token;
import org.scribe.oauth.OAuthService;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.HttpWebConnection;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class LinkedInExample {
private static final String AUTHORIZE_URL = "https://api.linkedin.com/uas/oauth/authorize?oauth_token=";
   //private static String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~/connections:(id,last-name)";
 
   private static String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~/network?type=SHAR&scope=self";

  
   public static void main(String[] args) throws Exception
   {
//IqTzOCsQ1U_XCXfeTVPTDjwwSq6qvtQNwMVLgy28KlPGNptWdyfFu-zXj8cABDAL
    String appkey = "";   //API Key
  //QIy3WwQs7x_S_vHgwjsY0YmBhgbDlQpGb4tB2l_jwAdq9hiHUx-7kD_-lcUZmCgs
    String appsecret = ""; //API Secret
     OAuthService service = new ServiceBuilder()
                                   .provider(LinkedInApi.class)
                                   .apiKey(appkey)
                                   .apiSecret(appsecret)
                                   .build();

     // Obtain the Request Token
     System.out.println("Fetching the Request Token...");
     Token requestToken = service.getRequestToken();

     String requestURL = AUTHORIZE_URL + requestToken.getToken();

     System.out.println(requestURL);
    
    
    
    
     final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
     webClient.setJavaScriptEnabled(true);
     webClient.setActiveXNative(true);
     webClient.setCssEnabled(true);
     webClient.setAppletEnabled(true);
    
     HttpWebConnection httpwebconnection = new HttpWebConnection(webClient); 
     httpwebconnection.setUseInsecureSSL(true); 
     webClient.setWebConnection(httpwebconnection);
    
    
    
     final HtmlPage page = webClient.getPage(requestURL);
     //System.err.println(page.asXml());
     HtmlTextInput key = (HtmlTextInput)page.getElementById("session_key-oauthAuthorizeForm");
     key.setValueAttribute("");//linkedin用户名
     System.err.println(key.asXml());
     HtmlPasswordInput password = (HtmlPasswordInput)page.getElementById("session_password-oauthAuthorizeForm");
     password.setValueAttribute("");//linkedin密码
     System.err.println(password.asXml());
    
     HtmlSubmitInput submit = (HtmlSubmitInput)(page.getElementsByName("authorize").get(0));
     System.err.println(submit.asXml());
     HtmlPage page2 = submit.click();
    
     System.out.println(page2.asXml());
    
     webClient.closeAllWindows();

   }
}



-------------------------------------------------------------
1。需要注册才可以。
//API Key
//API Secret
//linkedin用户名
//linkedin密码
2。不知道是什么地方出问题,总是登录不成功,不知道什么原因?请高手指导下,谢了!
出错提示为:
We’re sorry, there was a problem with your request. Please make sure you have cookies enabled and try again.

猜你喜欢

转载自letss.iteye.com/blog/1127456