Forum Discussion

nilesh1011's avatar
nilesh1011
Copper Contributor
Aug 05, 2019

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

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

9 Replies

  • Toby Statham's avatar
    Toby Statham
    Brass Contributor

    nilesh1011 Are you accessing it using spHttpClient in the page context to access the API? https://docs.microsoft.com/en-us/javascript/api/sp-http/sphttpclient?view=sp-typescript-latest

    • nilesh1011's avatar
      nilesh1011
      Copper Contributor

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

      • Toby Statham's avatar
        Toby Statham
        Brass Contributor

        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/

         

         

Resources