Microsoft Graph批量请求返回“Url specified is invalid”问题求助
解决Microsoft Graph批量请求返回"Url specified is invalid"错误的问题
问题场景
单个请求在Postman中单独发起GET请求,或单独调用callMsGraph(accessToken, graphEndpoint)时均能正常响应,但向批量请求地址https://graph.microsoft.com/v1.0/$batch发起POST请求时,返回400错误,提示"Url specified is invalid"。
传入的batchPayload(测试用2项)
{ requests: [ { id: '01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ', method: 'GET', url: 'https://graph.microsoft.com/v1.0/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ/thumbnails?select=large' }, { id: '01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG', method: 'GET', url: 'https://graph.microsoft.com/v1.0/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG/thumbnails?select=large' } ] }
收到的batchResponse
{ "responses": [ { "id": "01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG", "status": 400, "headers": { "Content-Type": "application/json" }, "body": { "error": { "code": "BadRequest", "message": "Url specified is invalid.", "innerError": { "date": "2023-05-17T18:17:39", "request-id": "7bb48670-f436-4c16-8137-9d70abe92dab", "client-request-id": "7bb48670-f436-4c16-8137-9d70abe92dab" } } } }, { "id": "01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ", "status": 400, "headers": { "Content-Type": "application/json" }, "body": { "error": { "code": "BadRequest", "message": "Url specified is invalid.", "innerError": { "date": "2023-05-17T18:17:39", "request-id": "7bb48670-f436-4c16-8137-9d70abe92dab", "client-request-id": "7bb48670-f436-4c16-8137-9d70abe92dab" } } } } ] }
调用的callMsGraph函数代码
function callMsGraph(accessToken, graphEndpoint, payload) { const headers = new Headers(); const bearer = `Bearer ${accessToken}`; headers.append("Authorization", bearer); headers.append("Content-Type", "application/json"); const options = { method: "POST", headers: headers, body: JSON.stringify(payload) }; return fetch(graphEndpoint, options) .then(response => response.json()) .... ... }
解决方案
问题根源在于批量请求的子请求URL使用了完整绝对路径,而Microsoft Graph批量接口要求子请求的URL必须是相对于批量端点(https://graph.microsoft.com/v1.0/$batch)的相对路径,需去掉https://graph.microsoft.com/v1.0前缀。
修改后的batchPayload如下:
{ "requests": [ { "id": "01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ", "method": "GET", "url": "/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ/thumbnails?select=large" }, { "id": "01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG", "method": "GET", "url": "/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG/thumbnails?select=large" } ] }
使用修改后的payload重新发起批量请求,即可正常获取响应。
内容的提问来源于stack exchange,提问作者TabulaRasa
相关产品推荐
相关产品推荐

