Wednesday, 17 July 2013

Using LinkedIn REST API to Share Content Programmatically via Scribe-Java OAuth Library

In this post, I will demonstrate how you can use LinkedIn REST API to Share Content programmatically via Scribe-Java OAuth Library.

To start with, you need to have an active LinkedIn account. Plus, you need to create a new LinkedIn Developer Application from LinkedIn Developer Site. If you want help on this, please refer to my previous post in which I demonstrate how you can create a new LinkedIn Developer Application, set different Scope(s) and how you can get following:

  1. API Key
  2. API Secret
  3. OAuth Token
  4. OAuth Secret

Do note that, for this example, you need to have 'rw_nus' scope because it is required to retrieve and post updates to LinkedIn as authenticated user. So make sure you have checked the relevant box against ‘rw_nus’.

A LinkedIn Share contains following elements:

  1. Comment
  2. Content – Title
  3. Content – URL
  4. Content – Description
  5. Content – Image URL
  6. Visibility

linkedin share

However, if your application can't provide all the metadata, LinkedIn will attempt to fetch the missing content for you. So we will only provide following and rest will be handled by LinkedIn itself:

  1. Comment – against the content you want to share
    • For example: “H-1B Work Visa USA - Everything you need to know - Info, Tips, Guides, Stats, News, Updates, Recommendations, Community, Jobs and much more!”
  2. URL – of the content you want to share
  3. Visibility

Here’s the code and inline explanation of what it does:

/**
* Code Of The Day - Using LinkedIn REST API to Share Content Programmatically via Scribe-Java OAuth Library
* http://codeoftheday.blogspot.com/2013/07/using-linkedin-rest-api-to-share.html
*/
package smhumayun.codeoftheday.linkedin.share;
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;
/**
* This class demonstrate how you can Share content via LinkedIn using Scribe-Java OAuth API
*
* User: smhumayun
* Date: 7/17/13
* Time: 5:03 PM
*/
public class ShareViaLinkedInExample {
/**
* Main Method
*
* @param args arguments
*/
public static void main(String[] args) {
//Instantiating the oAuth Service of Scribe-Java API
OAuthService oAuthService = new ServiceBuilder()
//LinkedIn Provider with Scopes support
.provider(LinkedInApi.withScopes(
//'rw_nus' is required to retrieve and post updates to LinkedIn as authenticated user
"rw_nus"
))
//API Key
.apiKey("XXXXXXXXXXXX")
//API Secret
.apiSecret("XXXXXXXXXXXXXXXX")
//build it!
.build();
//Instantiating oAuth Request of type POST and with LinkedIn Share REST API End Point URL
OAuthRequest oAuthRequest = new OAuthRequest(Verb.POST, "http://api.linkedin.com/v1/people/~/shares");
//Preparing XML payload to Share Content via LinkedIn
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" +
"<share> \n" +
" <comment>" +
//comments against the content you want to share
"H-1B Work Visa USA - Everything you need to know - Info, Tips, Guides, Stats, News, Updates, Recommendations, Community, Jobs and much more!" +
"</comment> \n" +
" <content> \n" +
" <submitted-url>" +
//URL of the content you want to share
"http://h1b-work-visa-usa.blogspot.com" +
"</submitted-url> \n" +
" </content> \n" +
" <visibility> \n" +
" <code>anyone</code> \n" +
" </visibility> \n" +
"</share>\n";
//add xml payload to request
oAuthRequest.addPayload(xml);
//sign your request with oAuth Service
oAuthService.signRequest(
new Token(
//OAuth Token
"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
//OAuth Token Secret
, "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
), oAuthRequest);
//set the content type header to text/xml - this is the type of content you are sending as payload
oAuthRequest.addHeader("Content-Type", "text/xml");
//send the request
Response response = oAuthRequest.send();
//print the response from server
System.out.println("response.getBody() = " + response.getBody());
}
}

Once your program ran successfully, you will see a response on your console, similar to following:

<!--
Code Of The Day - Using LinkedIn REST API to Share Content Programmatically via Scribe-Java OAuth Library
http://codeoftheday.blogspot.com/2013/07/using-linkedin-rest-api-to-share.html
-->
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<update>
<update-key>UNIU-XXXXXXXX-XXXXXXXXXXXXXXXXXXX-SHARE</update-key>
<update-url>http://www.linkedin.com/updates?discuss=&amp;scope=XXXXXXXX&amp;stype=M&amp;topic=XXXXXXXXXXXXXXXXXXX&amp;type=U&amp;a=7OVt</update-url>
</update>

You can use the update key to request the XML or JSON representation of the newly created share. This can be achieved by making a GET call to http://www.linkedin-ei.com/v1/people/~/network/updates/key={update_key} (setting {update_key} to the value you received in the previous response)

Alternatively, you can choose to to use the update url to redirect the user to the newly created share. This URL serves as a direct link to the posted share on LinkedIn.com so they can view the share in the browser.

No comments:

Post a Comment