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

时间:2019-07-17 19:55:29

标签: javascript node.js azure-active-directory microsoft-graph

我正在开发一个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);
    });
}
我要放在体内的

'manager'

{ 
   "@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"
}

我假设它存在某种解析问题,但是我不知所措。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

使其正常工作。

在请求中添加json: true,,正文必须为

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