gpt4 book ai didi

javascript - Request.put() JSON 错误(Node.JS、Microsoft Graph API - 分配管理器)

转载 作者:行者123 更新时间:2023-11-30 19:24:16 29 4
gpt4 key购买 nike

我正在开发一个 Node.js 应用程序,它向 Microsoft Graph API 发出请求以创建 Azure AD 用户。身份验证和创建用户工作正常。当我向 (https://graph.microsoft.com/v1.0/users/{id}/manager/$ref) 发出放置请求时,问题就来了。我收到此错误。

StatusCodeError: 400 - "{\r\n  \"error\": {\r\n    \"code\": \"Request_BadRequest\",\r\n    \"message\": \"An unexpected 'EndOfInput' node was found when reading from the JSON reader. A 'StartObject' node was expected.\",\r\n    \"innerError\": {\r\n      \"request-id\": \"6245c337-a157-49a8-9b59-bdd2bf5eea01\",\r\n      \"date\": \"2019-07-17T19:31:20\"\r\n    }\r\n  }\r\n}"
at new StatusCodeError (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request-promise-core\lib\errors.js:32:15)
at Request.plumbing.callback (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request-promise-core\lib\plumbing.js:104:33)
at Request.RP$callback [as _callback] (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request-promise-core\lib\plumbing.js:46:31)
at Request.self.callback (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
at Request.<anonymous> (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request\request.js:1161:10)
at Request.emit (events.js:198:13)
at IncomingMessage.<anonymous> (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:286:20)
at IncomingMessage.emit (events.js:203:15)

这是代码。我正在使用 request-promise-native。

 user.assignManager = function(id, manager){
auth.getToken().then(token => {
request.put('https://graph.microsoft.com/v1.0/users/' + id + '/manager/$ref', {
auth: {
bearer: token
},
body : manager,
headers: {
'Content-type': 'application/json'
}
}).then(value =>{
console.log('Assigned Manager');
}).catch(err =>{
console.log(err);
})
}).catch(err => {
console.log(err);
});
}

我放在 body 里的“经理”

{ 
"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#directoryObjects/$entity",
"@odata.type":"#microsoft.graph.user",
"id":"2db4c4e2-a349-4eb0-97d0-862143f5b01d",
"businessPhones":[],
"displayName":"John Smith",
"givenName":"John",
"jobTitle":"Intern",
"mail":null,
"mobilePhone":null,
"officeLocation":"BR",
"preferredLanguage":null,
"surname":"Smith",
"userPrincipalName":"JohnSmith@test1234512gmail.onmicrosoft.com"
}

我也试过这样而不是作为用户对象。

{
"id": "2db4c4e2-a349-4eb0-97d0-862143f5b01d"
}

我假设它存在某种解析问题,但我束手无策。有什么想法吗?

最佳答案

成功了。

在请求中添加json: true,,body需要为

{ "@odata.id": "https://graph.microsoft.com/v1.0/users/{id}" }

关于javascript - Request.put() JSON 错误(Node.JS、Microsoft Graph API - 分配管理器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57083247/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com