通过Microsoft Graph创建组返回错误“从JSON阅读器读取时发现了一个意外的PrimitiveValue节点”

时间:2017-12-05 10:03:35

标签: microsoft-graph

我试图通过Microsoft Graph使用我的管理员帐户创建group。我已经实现了所有必需的参数/变量,但是当我尝试调用API时,它会显示以下错误:

{
    "error": {
        "code": "Request_BadRequest",
        "message": "An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartArray' node was expected.",
        "innerError": {
            "request-id": "02bfcc43-5982-4c49-8484-da9d6cc61bab",
            "date": "2017-12-05T17:53:57"
        }
    }
}

我的代码:

<?php 
session_start();


echo $acc= $_SESSION['access_token'];

$data_string = array("description" => "An Awesome New Group", "displayName" => "Awesome Group", "groupTypes" => "Unified", "mailEnabled" => "true", "mailNickname" => "awesomeGroup", "securityEnabled" => "fasle"); 
$data = json_encode($data_string); 
//$data = $data_string; 
$curl = curl_init();

curl_setopt_array($curl, array( 
CURLOPT_URL => "https://graph.microsoft.com/v1.0/groups",

CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true, 
CURLOPT_ENCODING => "", 
CURLOPT_MAXREDIRS => 10, 
CURLOPT_TIMEOUT => 30, 
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
CURLOPT_CUSTOMREQUEST => "POST", 
CURLOPT_POSTFIELDS => "$data", 
CURLOPT_HTTPHEADER => array( 
"accept: application/json", 
"authorization: Bearer $acc", 
"content-type: application/json; charset=utf-8" 
), 
));

$response = curl_exec($curl); 
$err = curl_error($curl);

curl_close($curl);



print_r($response);




if ($err) { 
echo "cURL Error #:" . $err; 
} else { 
//echo $response; 
}
?>

请注意,我的真实租户ID已替换为admin@secx34fake.onmicrosoft.com,因此不会公开显示。

1 个答案:

答案 0 :(得分:0)

错误告诉您您使用的网址无效。

这是准确的,因为/v1.0/admin@secx34fake.onmicrosoft.com/groups不是有效的终点。具体而言,它拒绝userPrincipalName admin@secx34fake.onmicrosoft.com。我不确定你要去哪里,但Group不是User对象的孩子。用户可以是组的成员,但它们都是Active Directory中的顶级元素。

  

顺便说一句,&#34;段&#34;在URL的上下文中是由/分隔的元素。您可以将URL视为/分隔的字符串,每列都是&#34;段&#34;。例如,/v1.0/me/events包含3个细分:v1.0meevents。你得到的错误是告诉你其中一个&#34;段&#34;无效或不正确(即/v1.0/events/me具有错误顺序的正确段)。

对于creating a Group,正确的URI如下所示:

POST https://graph.microsoft.com/v1.0/groups

POST的正文中,您可以定义群组的属性。例如:

{
  "description": "An Awesome New Group",
  "displayName": "Awesome Group",
  "groupTypes": ["Unified"],
  "mailEnabled": true,
  "mailNickname": "awesomeGroup",
  "securityEnabled": false
}