6

I try to post new content using LinkedIn share API through JavaScript from AngularJS application as given below.

var xml = "<share><comment>" + content + "</comment><visibility><code>anyone</code></visibility></share>";

var req = {
   method: 'POST',
   url: 'https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=' + account.token,
   headers: {
       'Content-Type': 'text/plain'
   },
   data: xml
};

$http(req).success(function(data) {
   console.log(data);
   console.log('published to linkedin');
}).error(function() {
   console.log(arguments);
   console.log('failed to publish to linkedin');
});

I can successfully POST this data. However the browser blocks the response from being read because the response doesn't have an 'Access-Control-Allow-Origin' header.

Access-Control-Allow-Origin

But, I have given the http://localhost:3000 and 'https://localhost:3000' domains in LinkedIn application settings.

LinkedIn Application Settings

And the request/response in Chrome looks like this.

Failing request response

Any thoughts on how to be able to read the response and not let the browser block it?

I think the problem is the missing Access-Control-Allow-Origin header in the LinkedIn API response?

HoldOffHunger
  • 10,963
  • 6
  • 53
  • 100
Raju Mandapati
  • 681
  • 6
  • 20
  • You should look into the JS SDK, https://developer.linkedin.com/docs/js-sdk – CBroe Apr 08 '15 at 11:19
  • @ritesh: _“yes,you have to set the header”_ – he can not set that header for a service that he doesn’t have control over. – CBroe Apr 08 '15 at 11:20
  • @CBroe I am using the rest api documented at https://developer.linkedin.com/docs/rest-api. Not the JS SDK. – Raju Mandapati Apr 08 '15 at 11:27
  • Yeah, I got that. However, the JS SDK is provided specifically to make using their API from JavaScript easier … – CBroe Apr 08 '15 at 11:45
  • @CBroe I tried using the JS SDK. But I get a `400 Bad Request`. Coming back to REST API, why doesn't LinkedIn send an `Access-Control-Allow-Origin` header ? Am I missing something ? – Raju Mandapati Apr 08 '15 at 12:27

1 Answers1

10

Looks like LinkedIn's REST API doesn't support CORS. They suggest to use REST API from the backend and not from browser. JS-SDK must be used from the browser.

https://developer-programs.linkedin.com/forum/cors

Raju Mandapati
  • 681
  • 6
  • 20