2

I need to make a rest call to share a post in Linkedin and I am using Spring Social Linkedin module for that. Unfortunately I cannot simply use

org.springframework.social.linkedin.api.NetworkUpdateOperations.share(NewShare share)

method, I am working on a project that provides the raw rest template to the users, and users can make any rest call to any url using it. I am using Spring Social's Linkedin module just for the authentication part (and it works as it should).

So, I provide to users the rest template behind the Linkedin Spring Social Linkedin module. And they should be able to post a share to Linkedin with a given url and data in runtime. The request body should contain something like this (taken from here):

<share>
  <comment>Check out the LinkedIn Share API!</comment>
  <content>
    <title>LinkedIn Developers Documentation On Using the Share API</title>
    <description>Leverage the Share API to maximize engagement on user-generated content on LinkedIn</description>
    <submitted-url>https://developer.linkedin.com/documents/share-api</submitted-url>
    <submitted-image-url>http://m3.licdn.com/media/p/3/000/124/1a6/089a29a.png</submitted-image-url> 
  </content>
  <visibility> 
    <code>anyone</code> 
  </visibility>
</share>

To share, I create a String with that xml and use the command postForObject. Like this:

String toShare = "<share><comment>Check out..." // the string of xml
Object result = linkedinRestTemplate.postForObject("https://api.linkedin.com/v1/people/~/shares", toShare, Object.class);

But this call fails with a response 400 Bad Request. It seems like rest template handling this xml string and not as an object, so it is not serialized and put to the request body properly.

Spring Social Linkedin does the same thing but only with a difference: It has a serializable class called NewShare which have the same structure of that xml. But when an instance of the NewShare is given as body to the request, it is successful. Like this:

NewShare share = new NewShare();
share.set... // set its properties, sub-classes, content etc.
Object result = linkedinRestTemplate.postForObject("https://api.linkedin.com/v1/people/~/shares", newShare, Object.class);

This call is successful.

But I cannot deserialize my String into NewShare because I am providing an API so I assume I have absolutely no information about the request body.

So how can I manage to make spring handle the string xml body correctly and make a proper service call?

Utku Özdemir
  • 7,390
  • 2
  • 52
  • 49

0 Answers0