|
/** |
|
* 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()); |
|
|
|
} |
|
|
|
} |