azure MS Graph CalendarView BadRequest

k3bvogb1  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(87)

有人能告诉我为什么我在powershell脚本中使用此查询时会收到错误的请求。使用令牌进行身份验证工作正常,其他一些查询也使用相同的身份验证方法。

$query = "https://graph.microsoft.com/v1.0/[email protected]/calendarView?startDateTime=2023-10-03T00:00:00&endDateTime=2023-10-04T00:00:00"

$appointments =(Pocket-RestMethod-Headers @{Authorization =“Bearer $($PocketToken)"} -Uri $query -Method Get).Value
相同的查询在图形资源管理器中起作用。
错误消息:Invoke-RestMethod:远程服务器返回错误:(400)错误请求。
先谢谢你的帮助。

xqk2d5yq

xqk2d5yq1#

如果您传递来获取数据的查询无效,则通常会出现 “400 Bad request” 错误。

获取用户的日历视图,使用以下查询:

https://graph.microsoft.com/v1.0/users/[email protected]/calendarView?startDateTime=2023-10-02T19:00:00-08:00&endDateTime=2023-10-03T19:00:00-08:00
  • 由于我的环境中的用户没有任何日历视图,所以我得到的结果为空:*

当我在PowerShell中尝试与您相同的查询时,我得到了相同的错误,如下所示:

$query = "https://graph.microsoft.com/v1.0/[email protected]/calendarView?startDateTime=2023-10-01T19:00:00-08:00&endDateTime=2023-10-02T19:00:00-08:00"

若要解决错误,请确保在查询中包含**users**,并按如下方式修改:

$Body = @{
    client_id = "ClientID"
    client_secret = "ClientSecret"
    scope = "https://graph.microsoft.com/.default"
    grant_type = 'client_credentials'
}
$Connect_Graph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/TenantID/oauth2/v2.0/token" -Method Post -Body $Body
$token = $Connect_Graph.access_token
$query = "https://graph.microsoft.com/v1.0/users/[email protected]/calendarView?startDateTime=2023-10-01T19:00:00-08:00&endDateTime=2023-10-02T19:00:00-08:00"
(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $query -Method Get).value

参考:

List calendarView - Microsoft Graph v1.0

8i9zcol2

8i9zcol22#

日期和时间格式看起来不对,你可以传递ISO 8601格式的格式,如所述here
例如:$query = "https://graph.microsoft.com/v1.0/ [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) /calendarView?startDateTime=2023-10-03T00:00:00.000Z&endDateTime=2023-10-04T00:00:00.000Z"

相关问题