Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need to create a java code which gives me the number of followers of a company's linkedin page along with number of clicks that happen in the linkedin's page. First thing that i need to do is develop an Application Program Interface(API) . I created an api using developer site for linkedin. i got the keys. How am i supposed to run it in my java program?
Posted
Updated 6-Dec-13 19:02pm
v2
Comments
joginder-banger 7-Dec-13 13:27pm    
hello i have code in asp .net connect with linkeding. I am try my project this is a good working.if you need this code i will share and you can with the help of java script.

Download the following jars
--> Commons-codec-1.3.jar
--> signpost-core-1.1-SNAPSHOT.jar
and then the following program must work fine

Java
import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.net.HttpURLConnection;
import java.net.URL;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.signature.SignatureMethod;
public class Linkedin
{
	public static void main(String[] args) throws Exception {
    	OAuthConsumer consumer = new DefaultOAuthConsumer(
                "your linkedin key",
                "linkedin secret",
                SignatureMethod.HMAC_SHA1);

        OAuthProvider provider = new DefaultOAuthProvider(consumer,
                "https://api.linkedin.com/uas/oauth/requestToken",
                "https://api.linkedin.com/uas/oauth/accessToken",
                "https://api.linkedin.com/uas/oauth/authorize");


        System.out.println("Fetching request token from LinkedIn...");

        // we do not support callbacks, thus pass OOB
       
 String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);

 
       System.out.println("Request token: " + consumer.getToken());
   
     System.out.println("Token secret: " + consumer.getTokenSecret());

 
       System.out.println("Now visit:\n" + authUrl
                + "\n... and grant this app authorization");

        System.out.println("Enter the PIN code and hit ENTER when you're done:");


        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
       String pin = br.readLine();


        System.out.println("Fetching access token from LinkedIn...");


        provider.retrieveAccessToken(pin);


        System.out.println("Access token: " + consumer.getToken());

        System.out.println("Token secret: " + consumer.getTokenSecret());

 
       URL url = new URL("http://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-url,headline)");

        HttpURLConnection request = (HttpURLConnection) url.openConnection();

  
      consumer.sign(request);


        System.out.println("Sending request to LinkedIn...");
 
       request.connect();
 
       String responseBody = convertStreamToString(request.getInputStream());

        
        System.out.println("Response: " + request.getResponseCode() + " "
                + request.getResponseMessage() + "\n\n" + responseBody);
    }
    
   
 // Stolen liberally from http://www.kodejava.org/examples/266.html
    
public static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  
      StringBuilder sb = new StringBuilder();
 

        String line = null;
 
       try {
            while ((line = reader.readLine()) != null) {
 
               sb.append(line + "\n");
 

           }
        } 
catch (IOException e) {
            
e.printStackTrace();
 
       } finally {
     
       try {
         
       is.close();
    
        } catch (IOException e) {
 
               e.printStackTrace();
 
           }
        }
 
        return sb.toString();
   
 }
}
 
Share this answer
 
v2
Comments
coolRahul_12 9-Dec-13 1:16am    
Nice one :). Fits my requirement and works perfect
Rakshith Kumar 9-Dec-13 1:17am    
Thank you
Member 11204989 26-Apr-16 6:33am    
I used the same code but got the below error:
Exception in thread "main" oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.
at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:143)
at oauth.signpost.basic.DefaultOAuthProvider.retrieveRequestToken(DefaultOAuthProvider.java:79)
at Main.main(Main.java:34)
Consumer Key i gave as Client id but i am not getting what is signatures in this ??
Hi i have post a complete code for you. i have a get details of user when i connect with linkedin. This code in totally java script.
JavaScript
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: ****************
authorize: true
onLoad: onLinkedInLoad
scope: r_basicprofile r_emailaddress r_fullprofile
</script>
<script type="text/javascript">
    function onLinkedInLoad() {
        IN.Event.on(IN, "auth", onLinkedInAuth);
    }
    function onLinkedInAuth() {
        IN.API.Profile("me")
.fields("firstName", "lastName", "industry", "location:(name)", "picture-url", "headline", "summary", "num-connections", "public-profile-url", "distance", "positions", "email-address", "educations", "date-of-birth")
.result(displayProfiles)
.error(displayProfilesErrors);
    }

        function displayProfiles(profiles) {
            member = profiles.values[0];

           // alert(member.educations._total);
         // your School Name  alert(member.educations.values[0].schoolName);
           // 2010 alert(member.educations.values[0].startDate.year);

            //  BCA alert(member.educations.values[1].degree);
            // C.S.S University  alert(member.educations.values[1].schoolName);
          // Heigher Secondry  alert(member.educations.values[2].degree);
     // U.P.Board       alert(member.educations.values[2].schoolName);
        document.getElementById("lblName").innerHTML = member.firstName + " " + member.lastName + " Location is " + member.location.name;
        document.getElementById("imgProfile").src = member.pictureUrl;
        document.getElementById("lblEmail").innerHTML = member.emailAddress;
        document.getElementById("lblProfile").innerHTML = member.publicProfileUrl;
        document.getElementById("lblindustry").innerHTML = member.industry;
        document.getElementById("lbleducations").innerHTML = member.educations;
        document.getElementById("lblday").innerHTML = member.dateOfBirth.day;
        document.getElementById("lblmonth").innerHTML = member.dateOfBirth.month;
        document.getElementById("lblyear").innerHTML = member.dateOfBirth.year;
        document.getElementById("lbltotal").innerHTML = member.educations._total;

        document.getElementById("lbldegree").innerHTML = member.educations.values[0].degree;


        document.getElementById("lbldate").innerHTML = member.educations.values[0].endDate.year;
        document.getElementById("lblId").innerHTML = member.educations.values[0].id;
        document.getElementById("lblschool").innerHTML = member.educations.values[0].startDate.year;

//        member = profiles.values[1];
//        document.getElementById("lbldateOfBirth1").value = member.dateOfBirth.day;


    }
    function displayProfilesErrors(error) {
        profilesDiv = document.getElementById("profiles");
        profilesDiv.innerHTML = error.message;
        console.log(error);
    }
   
</script>

For any query left the comment.
 
Share this answer
 
v3
Comments
coolRahul_12 7-Dec-13 19:09pm    
nice one :). Thank you. I needed it in java though .
joginder-banger 7-Dec-13 22:14pm    
I think this is working in java language in also.
coolRahul_12 8-Dec-13 22:44pm    
nope :). Anyways thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900