skip to Main Content

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


  1. 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:

    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
    

    As the user in my environment doesn’t have any calendar view, I got the empty results:

    enter image description here

    When I tried the same query as you in PowerShell, I got the same error like below:

    $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"
    

    enter image description here

    To resolve the error, make sure to include users in the query and modify it like below:

    $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 
    

    enter image description here

    Reference:

    List calendarView – Microsoft Graph v1.0

    Login or Signup to reply.
  2. 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"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search