Can somone tell me why am i getting bad request using this query in powershell script.Authentication with token is working fine and some other query are working with same authentication method.
$query = "https://graph.microsoft.com/v1.0/[email protected]/calendarView?startDateTime=2023-10-03T00:00:00&endDateTime=2023-10-04T00:00:00"
$appointments = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($AccessToken)"} -Uri $query -Method Get).Value
Same query is working in graph explorer.
ErrorMessage : Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
Thanks in advance for your help.
2
Answers
The "400 Bad request" error usually occurs if the query you are passing to fetch the data is invalid.
To fetch the calendar view of the user, make use of below query:
As the user in my environment doesn’t have any calendar view, I got the empty results:
When I tried the same query as you in PowerShell, I got the same error like below:
To resolve the error, make sure to include
users
in the query and modify it like below:Reference:
List calendarView – Microsoft Graph v1.0
The date and time format looks wrong to me could you pass the format in ISO 8601 format as described here
Ex:
$query = "https://graph.microsoft.com/v1.0/[email protected]/calendarView?startDateTime=2023-10-03T00:00:00.000Z&endDateTime=2023-10-04T00:00:00.000Z"