question

GunawanSusanto-8711 avatar image
0 Votes"
GunawanSusanto-8711 asked GunawanSusanto-8711 edited

Ms Graph with php return error 400 Bad Request

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://docs.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-users
msgrpah01.png (77.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GunawanSusanto-8711 avatar image
0 Votes"
GunawanSusanto-8711 answered GunawanSusanto-8711 edited

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you! And good you were able to figure it out.

0 Votes 0 ·
TEJENDRAPRASADPATEL-1709 avatar image
0 Votes"
TEJENDRAPRASADPATEL-1709 answered TEJENDRAPRASADPATEL-1709 commented

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'

· 13
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry No Hope still producing same error. but if i try using graph explorer the result is shown.

0 Votes 0 ·

There is some progress :)...

Now can you share inner exception - i believe the way we passing from PHP code is doing it wrong...

Check full URL its being send as part of request...

1 more question - are you using same account in Code and Graph API ???


0 Votes 0 ·

I am not Expert in PHP, but basic logic is even though we apply filters it return Users object not User...

Try something below

 $users = $graph->createRequest("GET", "/users?\$filter=eq(mail, 'testaccount@mydomain.com')")
                                ->setReturnType(Model\User::class)
                                ->execute();
         foreach ($users as $user)
         {
             $this->assertEquals("A", substr($user->getDisplayName(), 0,1));
         }
0 Votes 0 ·

what is inner exception? have no idea


Q: 1 more question - are you using same account in Code and Graph API ???
A: Yes. is it a problem?.

0 Votes 0 ·

https://docs.microsoft.com/en-us/graph/tutorials/php

Itry tu build from the beginning. using this official tutorials but. also produce same error. its only error if i want to access other account. even for a basic task like get users/ but if i try to get everything under /me. its no problem is present

0 Votes 0 ·
Show more comments