Follow this link to download the jars & to get token, secret to work with LinkedIn API
Simple Java Program. To get the LinkedIn details about the current user [Who created the token & secret key]
public static void main(String[] args) {
String linkedinKey = "C2E7PG.."; //Your LinkedIn key
String linkedinSecret = "9hyC6.. ";//Your LinkedIn Secret
OAuthConsumer consumer = new DefaultOAuthConsumer( linkedinKey,linkedinSecret);
OAuthProvider provider = new DefaultOAuthProvider("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...");
String authUrl;
try {
authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
System.out.println("Check the below link and grant this app authorization -You will get Pin Number.Copy it\n" + authUrl );
System.out.println("Enter the PIN code and hit ENTER when you're done:");
} catch (OAuthMessageSignerException e1) {
e1.printStackTrace();
} catch (OAuthNotAuthorizedException e1) {
e1.printStackTrace();
} catch (OAuthExpectationFailedException e1) {
e1.printStackTrace();
} catch (OAuthCommunicationException e1) {
e1.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String pin;
try {
pin = br.readLine();
System.out.println("Fetching access token from LinkedIn...");
provider.retrieveAccessToken(consumer, 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);
request.connect();
System.out.println("Sending request to LinkedIn...");
String responseBody = convertStreamToString(request.getInputStream());
System.out.println("Response: " + request.getResponseCode() + " "
+ request.getResponseMessage() + "\n" + responseBody);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String convertStreamToString(InputStream is) {
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();
}
}
Output
Fetching request token from LinkedIn...
Check the below link and grant this app authorization -You will get Pin Number.Copy it
https://api.linkedin.com/uas/oauth/authorize?oauth_token=dcb835667-a5d7-4e95-91f9-fde4b9b47e3a
Enter the PIN code and hit ENTER when you're done:
59278
Fetching access token from LinkedIn...
Access token: f0f94b3d-50b9-4549-bd96-5d1ea3ba7a9c
Token secret: f2b13c99-f206-4612-bf9e-2afc1387bc1a
Sending request to LinkedIn...
Response: 200 OK
GtYKzPiNkX
Sundari Shree
Gunasekaran
Software Engineer at Vision Tech Solutions
Note: By passing the different URL’s as parameter You can check all the available information in LinkedIn .
URL url = new URL("http://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-url,headline)");
This URL is to get about the people information.
Hi Shree,
ReplyDeleteI have work out your code but it shows the error in the following line
authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
ERROR: method retrieveRequestToken in interface oauth.signpost.OAuthProvider cannot be applied to given types
required: java.lang.String
found: oauth.signpost.OAuthConsumer,java.lang.String
--
(Alt-Enter shows hints)
Please help me on this
how to avoid enter pin system
ReplyDeleteplz reply as you can
ReplyDeleteplz reply as you can
ReplyDeletecan u please help me to get the list of company page the user has
ReplyDelete