0

我需要获取列表的项目,所以我创建了这个函数

export function retrieveSPItems(spToken, alias) {
  var url = `{path_to_my_site}/_api/web/Lists/getByTitle('Briefs')/ItemCount`;
  var myHeaders = new Headers({
    Accept: "application/json;odata=nometadata",
    Authorization: spToken,
  });
  return fetch(url, {
    method: "get",
    headers: myHeaders,
  }).then((response) => response.json());
}

作为输出,我得到 3000。

当我将网址更改为

var url = `{path_to_my_site}/_api/web/Lists/getByTitle('Briefs')/Items`;

我得到一个空列表!

PS

它在邮递员中工作没有问题

令牌由 adaljs 生成:

获取令牌

authContext.acquireToken(SP_BASE_URL, function (error, token){....})

阿达尔配置

export const adalConfig = {
  tenant: CURRENT_TENANT,
  clientId: CURRENT_APP_ID,
  endpoints: {
    api: CURRENT_APP_ID,
    graph: GRAPH_BASE_URL,
    sharepoint: SP_BASE_URL,
  },
  cacheLocation: "localStorage",
  validateAuthority: true,
};

所以我需要知道:

  • 这个问题的原因是什么?

  • 我该如何解决?

4

1 回答 1

1

信息太笼统,需要调试并弄清楚详细的错误信息。

我的测试演示:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="Scripts/adal.js"></script>
    <script type="text/javascript">

        var authContext = null;
        var user = null;

        (function () {
            window.config = {
                instance: 'https://login.microsoftonline.com/',
                tenant: 'xxx.onmicrosoft.com',
                clientId: '9afc37cb-x-x-x-xxx',
                postLogoutRedirectUri: window.location.origin,
                endpoints: {
                    graphApiUri: "https://graph.microsoft.com",
                    sharePointUri: "https://xxx.sharepoint.com/",
                },
                    cacheLocation: 'localStorage' // enable this for IE, as sessionStorage does not work for localhost.
            };

            authContext = new AuthenticationContext(config);
            var isCallback = authContext.isCallback(window.location.hash);
            authContext.handleWindowCallback();
            //$errorMessage.html(authContext.getLoginError());
            if (isCallback && !authContext.getLoginError()) {
                window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
            }

            user = authContext.getCachedUser();
            if (!user) {
                authContext.login();
            }
            //authContext.acquireToken(window.config.clientId, function (error, token) {
            //    console.log('---');
            //})
            authContext.acquireToken(window.config.endpoints.sharePointUri, function (error, token) {
                alert(token);
                if (error || !token) {
                    console.log("ADAL error occurred: " + error);
                    return;
                }
                else {
                    var listUri = window.config.endpoints.sharePointUri + "sites/lee/_api/web/lists/GetByTitle('mylist')/items?$select=Title";

                    $.ajax({
                        type: "GET",
                        url: listUri,
                        headers: {
                            "Authorization": "Bearer " + token,
                            "accept": "application/json;odata=verbose"
                        }
                    }).done(function (response) {
                        console.log("Successfully fetched list from SharePoint.");
                        var items = response.d.results;
                        for (var i = 0; i < items.length; i++) {
                            console.log(items[i].Title);
                            $("#SharePoint").append("<li>" + items[i].Title + "</li>");
                        }
                    }).fail(function () {
                        console.log("Fetching list from SharePoint failed.");
                    })
                }
            })

        }());
    </script>

在此处输入图像描述

于 2020-06-03T07:09:25.587 回答