Wednesday, 10 December 2014

How To Post On LinkedIn Using Java

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

public class ShareLinkedIn {
     public static void main(String[] args) {                                         
          OAuthService oAuthService = new ServiceBuilder()       
           .provider(LinkedInApi.class)       
           .apiKey("xxxxxxxxxxxxxxx")    
           .apiSecret("xxxxxxxxxxxxxx")       
           .build();
       
        OAuthRequest oAuthRequest = new OAuthRequest(Verb.POST, "http://api.linkedin.com/v1/people/~/shares");
       
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" +
        "<share> \n" +
        " <comment>" +
             "Hello!" +
        "</comment> \n" +
        " <content> \n" +
        " <submitted-url>" +
           "http://www.google.com" +
        "</submitted-url> \n" +
        " </content> \n" +
        " <visibility> \n" +
        " <code>anyone</code> \n" +
        " </visibility> \n" +
        "</share>\n";
             
        oAuthRequest.addPayload(xml);
       
        oAuthService.signRequest(
       
        new Token("xxxxxxxxxxxxxxxxxxxxxxxxx"     
        , "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"       
        ), oAuthRequest);
       
       oAuthRequest.addHeader("Content-Type", "text/xml");
       
        Response response = oAuthRequest.send();
       
       System.out.println("response.getBody() = " + response.getBody());
       
        }
   

}