CORS error occuring after accessing linkedin share api through modern script on SharePoint Online

Highlighted
Occasional Contributor

I am trying to add LinkedIn company page feed on my SharePoint Online page. But facing CORS problem. Same linkedIn share api is working fine when tested on Postman and Curl. I have also tried using with nodejs. It is showing desired result in all the cases. But when I use it in SharePoint Online, using modern script editor then it shows CORS problem.

function myFunction()
{
var accessToken='************';
var requestUri = "<LinkedIn Share API URL>";
       $.ajax({
          url: requestUri,
          type: "GET",
          async: false,
          data:{count: 1},
          headers: {
            'Authorization': `Bearer ${accessToken}`,
            'cache-control': 'no-cache',
            'Access-Control-Allow-Origin':'*'
          },
          success: onSuccess,
          error: onError
      });
function onSuccess(data) 
{
    console.log("success: "+JSON.stringify(data));
}
function onError(error) {
    console.log('LinkedIn Error: '+JSON.stringify(error));
 }
}

 

Console Error:

Access to XMLHttpRequest at 'http://api.linkedin.com/v2/shares?q=owners&owners=urn:li:organization:********&sharesPerOwner=1&count=1&count=1' from origin 'https://spprojectstwo.sharepoint.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

8 Replies
Highlighted
Highlighted

@Toby Statham I am accessing linkedIn share api through jquery Ajax method in sharePoint online modern script editor.

Highlighted

Not sure exactly what you're trying to do, but this doesn't look like the correct way to access the LinkedIn APIs. Where did you get this example from? 

 

Here is the developer documentation for LinkedIn - https://docs.microsoft.com/en-us/linkedin/consumer/

 

 

Highlighted

@Toby Statham 

We are using the webpart with No Javascript language now. When we are trying to connect the Share web api of the LinkedIn. It is giving the CORS error. I'm also attached the No Javascript typescript file for you info. API call is in the LoadData() function.

Kindly provide the help asap.

Highlighted

@nilesh1011 Have you added the tenant URL as an SDK in your LinkedIn application as detailed here - https://developer.linkedin.com/docs/getting-started-js-sdk

Highlighted
Highlighted

@nilesh1011 I wasn't referring to the SDK, just the section you need to add your tenant URL to. 

 

It's slightly changed, but you need to add your tenant domain to the Widgets section in your LinkedIn app settings.

Highlighted

@Toby Statham Yes! I have added tenant domain to the Widgets section in my LinkedIn app settings. But still showing cors problem.

 

Widgets.JPG