日期:2014-5-17 浏览次数:20100次

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");
 &n