javascript 使用图形API过滤结果时出现400错误

xhv8bpkk  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(85)

给定这些URL:

const urls = {
    demos: GRAPH_ENDPOINT + `sites/${ids.siteMain}/lists/${ids.listDemos}/items?expand=fields`,
    demosScheduled: GRAPH_ENDPOINT + `sites/${ids.siteMain}/lists/${ids.listDemos}/items?expand=fields&$filter=fields/isScheduled eq 1`,
}

第一个命令运行良好,它列出了sharepoint列表中的所有条目,但是第二个命令返回了400 (Bad Request)错误,在调试控制台中,它如下所示:
/项目?扩展=字段& $筛选=字段/已安排%20eq%20%271%27
我试过带引号和不带引号(')。在Graph Explorer中两种方式都有效,并且只返回预期的结果。事实上,如果我从调试控制台复制完整的URL并将其粘贴到图形资源管理器中,即使使用HTML编码也能正常工作。
相关代码:

function callMSGraph(endpoint, token, callback) {
    const headers = new Headers();
    const bearer = `Bearer ${token}`;

    headers.append("Authorization", bearer);

    const options = {
        method: "GET",
        headers: headers
    };

    fetch(endpoint, options)
        .then(response => response.json())
        .then(response => callback(response, endpoint))
        .catch(error => console.log(error));
}

function getDemos() {
    getTokenPopup(tokenRequest)
        .then(response => {
            //this does not work
            callMSGraph(urls.demosScheduled, response.accessToken, ui.displayDemos);
            //this does work
            callMSGraph(urls.demos, response.accessToken, ui.displayDemos);
        }).catch(error => {
            console.error(error);
        });
}
bmp9r5qi

bmp9r5qi1#

请尝试使用JavaScript SDKs,并使用以下查询运行相同的查询

let items = await client.api('/sites/root/lists/list_id/items?expand=fields&$filter=fields/Title+eq+\'Help+request\'')
    .header('Prefer','HonorNonIndexedQueriesWarningMayFailRandomly')
    .filter('fields/isScheduled eq \1 ')
    .get();

相关问题