Ms Graph with php return error 400 Bad Request

Gunawan Susanto 96 Reputation points
2021-05-14T10:07:22.763+00:00

Hey Guys i try to using MsGraph for doing some administrative work. its work for more than 4-5 month ago but when i try to get back to my code i even cant access the simple API call and returning error 400
96639-msgrpah01.png

but when i try to access my own (/me) it can return the correct result 200(ok)
but when i try to use /users/testaccount@mydomian.com its returning 400

its using PHP with Laravel.

this the source code

    public function Gettest()  
    {   
    
      $viewData = $this->loadViewData();  
      
      // Get the access token from the cache  
      $tokenCache = new TokenCache();  
      $accessToken = $tokenCache->getAccessToken();  
    
       
      $graph = new Graph();  
      $graph->setAccessToken($accessToken);  
    
      //$user = $graph->createRequest("GET", "/me")   //<-- This One Works   
      $user = $graph->createRequest("GET ", "/users/testaccount@mydomian.com")   //NOT WORK   
      ->addHeaders(array("Content-Type" => "application/json"))  
      ->setReturnType(Model\User::class)  
        ->execute();  
      dd($user);  
    
        
    }  

this an example code taken from
https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http

i also do the double check and already unlock the admin consent and permission required.

is there any thing that i do wrong?. because is almost on production then suddenly. it doesn't work even for the simplest API call from the documentation.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,715 questions
0 comments No comments
{count} votes

Accepted answer
  1. Gunawan Susanto 96 Reputation points
    2021-05-30T07:20:22.477+00:00

    ITS SOLVED.
    Oh my. its just because there were a SPACE after GET

    $user = $graph->createRequest("GET ", "/users/testaccount@mydomian.com") //NOT WORK
    $user = $graph->createRequest("GET", "/users/testaccount@mydomian.com") //NOW its WORK

    Thank you for all the answer really appreciated. i upgrading my XAMPP to the newer version. i think the new one have a strict rules about space. i don't really understand how it happened but. i hope can help the others with the same problem.


1 additional answer

Sort by: Most helpful
  1. TEJENDRA PRASAD PATEL 111 Reputation points
    2021-05-15T16:49:38.237+00:00

    Have you given permission - User.Read.All

    And your API call should be as below

    GET https://graph.microsoft.com/v1.0/users?$filter=mail eq 'testaccount@mydomian.com'