Unable to pull rest data from SharePoint using angularJS $http service

683 Views Asked by At

I am trying to pull list data from a SharePoint list called List1 in a content editor web part. I have been able to do this in the past without any problems, but for some reason I only get 401 error when I try to do it now. When I paste the Url for the get request into the browser it pulls the data as expected. Why is SharePoint Online blocking my browser from pulling the same data? I have also tested this with an ajax call and received the same result.

Error in console:

HTTP401: DENIED - The requested resource requires user authentication.
GET - https://portal.office.com/SuiteServiceProxy.aspx?upn=MSoporito%40MichaelSoporito.com&suiteServiceReturnUrl=https%3A%2F%2Fmsoap.sharepoint.com%2FSitePages%2Ftest.aspx&returnUrl=https%3A%2F%2Fmsoap.sharepoint.com%2FSitePages%2Ftest.aspx&Silent=1

Code:

(function(angualr){
    var appVar = angular.module('listApp', []); 

    appVar.controller("controller1", function($scope, $http){    
        var secondUrl = "https://msoap.sharepoint.com/_api/Web/Lists(guid'42540d2d-df19-477f-a57b-837d9de15641')/Items?$select=Title"  
        $http({    
            url: secondUrl,    
            method: "GET",       
            headers: {
                "accept": "application/json;odata=verbose", 
                "content-Type": "application/json;odata=verbose" 
            },
            success: function(data){   
                $scope.items = data.d.results;    
            },    
            error: function(sender,args){    
                console.log(args);    
            }    
        });      
    }); 
})(window.angular); 

Http:

<div ng-app="listApp">    
    <div id="App1" ng-controller="controller1">    
        <h1>First List Items</h1>    
        <div ng-repeat="item in items">    
            <p>{{item.Title}}</p>    
        </div>    
    </div>        
</div>   

<script src="https://msoap.sharepoint.com/SiteAssets/jquery-3.1.0.min.js"></script>
<script src="https://msoap.sharepoint.com/SiteAssets/angular.min.js"></script>
<script src="https://msoap.sharepoint.com/SiteAssets/script.js"></script>
1

There are 1 best solutions below

1
gruss On

You may be facing the same issue here: https://sharepoint.stackexchange.com/questions/115015/sharepoint-search-rest-api-401-unauthorized-error-need-help

In SharePoint Online, you have to pass in certain credentials to get through the authentication.