As a sequel to my earlier post, Programmatically Posting to LinkedIn Groups via LinkedIn REST API and using Scribe-Java OAuth Library, in this post I will demonstrate how you can Flag a Post or a Discussion in a LinkedIn Group as a Promotion or a Job using Scribe and Java.
Consider the output of our example program in my previous post:
# 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 |
Notice the following header and its value:
Location = http://api.linkedin.com/v1/posts/g-5046253-S-259167773
This URL contains the post id of the published post and it is the very last part of the URL “g-5046253-S-259167773”.
So, what we will do now is the extend the same program/example and will try to extract the “Location” header value and then extract the “post-id” from that value:
//http://api.linkedin.com/v1/posts/g-5046253-S-276447044
String preUrl = "http://api.linkedin.com/v1/posts/";
String url = response.getHeader("Location").substring(preUrl.length());
url = preUrl + url + "/category/code";
Once you have the target URL, you can now proceed with the HTTP PUT request, using the url as a LinkedIn REST API end point.
OAuthRequest request = new OAuthRequest(Verb.PUT, url);
request.addHeader("Content-Type", "text/xml");
Now, if you want to Flag your Post or Discussion as a Promotion, you should add following XML stanza as the pay load to this request:
request.addPayload("<code>promotion</code>");
OR, if you want to Flag your Post or Discussion as a Job, you should add following XML stanza as the pay load to this request:
request.addPayload("<code>job</code>");
And, finally send the request to LinkedIn REST API server:
signOAuthRequest(request);
response = request.send();
/** | |
* Code Of The Day - How to Flag a Post or a Discussion in a LinkedIn Group as a Promotion or a Job using Scribe and Java | |
* - http://codeoftheday.blogspot.com/2013/09/how-to-flag-post-or-discussion-in.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: 9/25/13 | |
* Time: 1:03 PM | |
*/ | |
public class PostToLinkedInGroupWithFlaggingExample { | |
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)); | |
//http://api.linkedin.com/v1/posts/g-5046253-S-276447044 | |
String preUrl = "http://api.linkedin.com/v1/posts/"; | |
String url = response.getHeader("Location").substring(preUrl.length()); | |
url = preUrl + url + "/category/code"; | |
System.out.println("url = " + url); | |
OAuthRequest request = new OAuthRequest(Verb.PUT, url); | |
request.addHeader("Content-Type", "text/xml"); | |
request.addPayload("<code>job</code>"); | |
signOAuthRequest(request); | |
response = request.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); | |
} | |
} |
When you run this updated program, you will see an output similar to the following on your console:
# Code Of The Day - How to Flag a Post or a Discussion in a LinkedIn Group as a Promotion or a Job using Scribe and Java | |
# - http://codeoftheday.blogspot.com/2013/09/how-to-flag-post-or-discussion-in.html | |
response.getBody() = | |
response.getMessage() = Created | |
response.getCode() = 201 | |
response.getHeaders(): | |
null = HTTP/1.1 201 Created | |
Age = 2 | |
X-LI-UUID = 8CfT2IcuJxNQvy+ELCsAAA== | |
Date = Wed, 25 Sep 2013 15:29:31 GMT | |
Vary = Accept-Encoding | |
Content-Length = 0 | |
X-Li-Fabric = PROD-ELA4 | |
Location = http://api.linkedin.com/v1/posts/g-5046253-S-276449994 | |
x-li-request-id = N3JMYKSBLB | |
X-Li-Pop = PROD-ELA4 | |
Connection = keep-alive | |
Server = Apache-Coyote/1.1 | |
url = http://api.linkedin.com/v1/posts/g-5046253-S-276449994/category/code | |
response.getBody() = | |
response.getMessage() = No Content | |
response.getCode() = 204 | |
response.getHeaders(): | |
null = HTTP/1.1 204 No Content | |
Age = 0 | |
X-LI-UUID = yM8OSoguJxNQOBCwhSsAAA== | |
Date = Wed, 25 Sep 2013 15:29:33 GMT | |
X-Li-Fabric = PROD-ELA4 | |
x-li-request-id = CT86R1S8EV | |
X-Li-Pop = PROD-ELA4 | |
Connection = close | |
Server = Apache-Coyote/1.1 |
The Code 204 is fine, in our case as it complains about “No Content”. Your Post or Discussion on a LinkedIn Group has been Flagged as a Promotion or a Job.