json Microsoft Graph PowerShell Invoke-RestMethod用于POST到包含多个事件的日历

w6mmgewl  于 2023-06-25  发布在  Shell
关注(0)|答案(3)|浏览(124)

我有以下PowerShell脚本,它在Office 365组日历上添加事件。当我运行下面的脚本时,它只会添加第一个事件,而不会添加第二个事件。而且没有错误。我在剧本上错过了什么吗?任何对此的帮助都将不胜感激,谢谢。

$json = @"
  {
    "subject": "Event1",
    "start": {
      "timeZone": "UTC",
      "dateTime": "2023-06-10T08:00"
    },
    "end": {
      "timeZone": "UTC",
      "dateTime": "2023-06-10T09:00"
    }
  },
  {
    "subject": "Event2",
    "start": {
      "timeZone": "UTC",
      "dateTime": "2023-06-11T08:00"
    },
    "end": {
      "timeZone": "UTC",
      "dateTime": "2023-06-11T09:00"
    }
  }
"@
$apiUrl = "https://graph.microsoft.com/v1.0/groups/xxxx-xxxx-xxxx-xxx/events"
Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Post -ContentType "application/json" -Body $json
ctrmrzij

ctrmrzij1#

您不能使用Graph API一次创建多个事件,要创建多个事件,您必须使用批处理,请参阅此link了解更多信息:
请参见此处的示例:

{  
  "requests": [  
    {  
      "id": "1",  
      "method": "POST",  
      "url": "/groups/{id}/calendar/events",  
      "body": { "event 1 body here" },  
      "headers": {  
        "Content-Type": "application/json"  
      }  
    },  
    {  
      "id": "2",  
      "method": "POST",  
      "url": "/groups/{id}/calendar/events",  
      "body": {"event 2 body here"},  
      "headers": {  
        "Content-Type": "application/json"  
      }  
    }
  ]  
}
idfiyjo8

idfiyjo82#

端点https://graph.microsoft.com/v1.0/groups/xxxx-xxxx-xxxx-xxx/events只允许添加一个事件。第二个事件被忽略。
使用batch端点

$json = @"
{
    "requests": [
        {
            "url": "/groups/xxx/events",
            "method": "POST",
            "id": "1",
            "body": {
                "subject": "Event1",
                "start": {
                "timeZone": "UTC",
                "dateTime": "2023-06-10T08:00"
                },
                "end": {
                "timeZone": "UTC",
                "dateTime": "2023-06-10T09:00"
                }
            },
            "headers": {
                "Content-Type": "application/json"
            }
        },
        {
            "url": "/groups/xxx/events",
            "method": "POST",
            "id": "1",
            "body": {
                "subject": "Event2",
                "start": {
                "timeZone": "UTC",
                "dateTime": "2023-06-11T08:00"
                },
                "end": {
                "timeZone": "UTC",
                "dateTime": "2023-06-11T09:00"
                }
            },
            "headers": {
                "Content-Type": "application/json"
            }
        }
    ]
}
"@
$apiUrl = "https://graph.microsoft.com/v1.0/`$batch"
Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Post -ContentType "application/json" -Body $json
wlwcrazw

wlwcrazw3#

我收到以下错误

Response status code does not indicate success: 405 (Method Not Allowed).

相同的json从https://developer.microsoft.com/en-us/graph/graph-explorer工作正常

$json = @"
{
    "requests": [
        {
            "url": "/groups/xxx/events",
            "method": "POST",
            "id": "1",
            "body": {
                "subject": "Event1",
                "start": {
                    "timeZone": "UTC",
                    "dateTime": "2023-06-10T08:00"
                },
                "end": {
                    "timeZone": "UTC",
                    "dateTime": "2023-06-10T09:00"
                }
            },
            "headers": {
                "Content-Type": "application/json"
            }
        },
        {
            "url": "/groups/xxx/events",
            "method": "POST",
            "id": "2",
            "body": {
                "subject": "Event2",
                "start": {
                    "timeZone": "UTC",
                    "dateTime": "2023-06-10T08:00"
                },
                "end": {
                    "timeZone": "UTC",
                    "dateTime": "2023-06-10T09:00"
                }
            },
            "headers": {
                "Content-Type": "application/json"
            }
        }
    ]
}
"@
$apiUrl = "https://graph.microsoft.com/v1.0/$batch"
Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Post -ContentType "application/json" -Body $json

相关问题