Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

javascript - CORS blocks LinkedIn share API

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...