In this post, I will demonstrate how you can post to LinkedIn Groups programmatically via LinkedIn REST API and using 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:
- API Key
- API Secret
- OAuth Token
- OAuth Secret
Do note that, for this example, you need to have 'rw_groups' scope because it is required to retrieve and post group discussions as authenticated user. So make sure you have checked the relevant box against ‘rw_groups’.
A LinkedIn Group Post contains following elements:
- Post Title
- Post Summary
- Content URL
- Content Image URL
- Content Title
- Content Description
Here’s the code and inline explanation of what it does:
/** | |
* Code Of The Day - Programmatically Posting to LinkedIn Groups via LinkedIn REST API and using Scribe-Java OAuth Library | |
* http://codeoftheday.blogspot.com/2013/07/programmatically-posting-to-linkedin.html | |
*/ | |
package smhumayun.codeoftheday.linkedin.group; | |
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 Post Content to LinkedIn Groups using Scribe-Java OAuth API | |
* | |
* User: smhumayun | |
* Date: 7/18/13 | |
* Time: 7:03 PM | |
*/ | |
public class PostToLinkedInGroupExample { | |
private static OAuthService oAuthService; | |
/** | |
* Main Method | |
* | |
* @param args arguments | |
*/ | |
public static void main(String[] args) { | |
//Instantiating the oAuth Service of Scribe-Java API | |
oAuthService = new ServiceBuilder() | |
//LinkedIn Provider with Scopes support | |
.provider(LinkedInApi.withScopes( | |
//'rw_groups' is to retrieve and post group discussions as authenticated user | |
"rw_groups" | |
)) | |
//API Key | |
.apiKey("XXXXXXXXXXXX") | |
//API Secret | |
.apiSecret("XXXXXXXXXXXXXXXX") | |
//build it! | |
.build(); | |
//Instantiating oAuth Request of type POST and with LinkedIn Group Discussion Post - REST API End Point URL | |
OAuthRequest postGroupDiscussionRequest = new OAuthRequest(Verb.POST, "http://api.linkedin.com/v1/groups/5046253/posts"); | |
//Preparing XML payload to Share Content on LinkedIn Group | |
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" + | |
"<post>\n" + | |
"<title>" + | |
//title of the post | |
"New Job Post!" + | |
"</title>\n" + | |
"<summary>" + | |
//summary of the post | |
"A new job post has been shared recently by Syed Muhammad Humayun on H-1B Work Visa USA" + | |
"</summary>\n" + | |
"<content>\n" + | |
"<submitted-url>" + | |
//url of the content | |
"http://h1b-work-visa-usa.blogspot.com/2013/07/h-1b-transfer-jobs-java-tech-lead-il.html" + | |
"</submitted-url>\n" + | |
"<submitted-image-url>" + | |
//image url of the content (if any) | |
"http://lh4.ggpht.com/-X2DLlJm6hyg/Uec5z_tRxxI/AAAAAAAAEP8/E3gVAGKIYPs/h-1b%252520transfer%252520green%252520card%252520processing%252520jobs%25255B4%25255D.png?imgmax=800" + | |
"</submitted-image-url>\n" + | |
"<title>" + | |
//title of the content | |
"H-1B Transfer Jobs - Java Tech Lead - IL - (Sponsor Green Card)" + | |
"</title>\n" + | |
"<description>" + | |
//description of the content | |
"Provides application development and support to partner in the planning, delivery and/or support of business processes utilizing information technology and business practices for strategic business units. Work is of medium to high complexity and moderate to high in risk. Has expanded contact with responsibility to varied and multiple departments and functional operations, and actively participates in strategic business relationships. Serves as a key team member which may include being on multiple teams and/or team lead. Participates in the review and formation of processes. May plan work and schedules for others for project related work. Impact of decision-making is medium to high risk and impact. Serves as a consultant or expert and actively shares knowledge across workgroups. Applies information analyses to optimize the integration of major strategic business processes. Designs and implements complex changes impacting several processes with minimal direction. Primarily performs as an individual contributor, but may supervise a small work team (6 or fewer members). Duties: Lead the Identification, analysis and selection of complex information technology and business practices to support strategic business process/plans. Participates as required to design, develop, test and integrate applications of high complexity. Lead in the implementation of information technology and business processes of high complexity. Supports, evaluates, and continuously improves information technology and business processes to maintain alignment with business plans of medium-high complexity and medium-high risk. Leads the development and may manage a project plan and schedule for a given functional area. Acquires solid foundation of project management. Engages in expanded contact with varied and multiple departments and functional operations; actively participating in strategic business relationships and/or issues." + | |
"</description>\n" + | |
"</content>\n" + | |
"</post>\n"; | |
//set the content type header to text/xml - this is the type of content you are sending as payload | |
postGroupDiscussionRequest.addHeader("Content-Type", "text/xml"); | |
//add xml payload to request | |
postGroupDiscussionRequest.addPayload(xml); | |
//sign oAuth request | |
signOAuthRequest(postGroupDiscussionRequest); | |
//send the request | |
Response response = postGroupDiscussionRequest.send(); | |
//print the response from server | |
System.out.println("response.getBody() = " + response.getBody()); | |
System.out.println("response.getMessage() = " + response.getMessage()); | |
System.out.println("response.getCode() = " + response.getCode()); | |
System.out.println("response.getHeaders():"); | |
for(String headerKey : response.getHeaders().keySet()) | |
System.out.println("\t" + headerKey + " = " + response.getHeader(headerKey)); | |
} | |
/** | |
* Sign given oAuthRequest with oAuth Service | |
* | |
* @param oAuthRequest oAuthRequest | |
*/ | |
public static void signOAuthRequest (OAuthRequest oAuthRequest) | |
{ | |
//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); | |
} | |
} |
Once your program ran successfully, you will see a response on your console, similar to following:
# Code Of The Day - Programmatically Posting to LinkedIn Groups via LinkedIn REST API and using Scribe-Java OAuth Library | |
# http://codeoftheday.blogspot.com/2013/07/programmatically-posting-to-linkedin.html | |
response.getBody() = | |
response.getMessage() = Created | |
response.getCode() = 201 | |
response.getHeaders(): | |
null = HTTP/1.1 201 Created | |
Date = Thu, 18 Jul 2013 18:46:12 GMT | |
Vary = Accept-Encoding | |
Content-Length = 0 | |
Location = http://api.linkedin.com/v1/posts/g-5046253-S-259167773 | |
x-li-request-id = 7R09I1AUHX | |
X-LI-R2-W-IC-2 = com.linkedin.container.dsc=1 | |
Server = Apache-Coyote/1.1 |
No comments:
Post a Comment