Microsoft Graph API - working example

Hi,

Has someone a working example accessing MS Graph API with yii?

MS offers a example for laravel (including callback function etc.):

Best regards,
Toby

Hello,

Are you trying to accomplish anything specific with the MS Graph API? I have used it fairly extensively. Im not sure if im using it in the most optimal way, but here is what I have:

Inside my own custom class I have a function to get all the users in a AD group:

public function getGroupUsers($groupID, $nextUrl = null) {

  $token = $this->_token();

   if ($token) {
     $graph = new Graph();
     $graph->setAccessToken($token);

     if ($nextUrl) {
      $url = $nextUrl;
     } else {
      $url = "https://graph.microsoft.com/v1.0/groups/$groupID/members";
     }

     $data = $graph->createRequest("GET", $url)
                  // ->setReturnType(Model\Group::class)
                   ->execute();
    }

    $next = $data->getBody()['@odata.nextLink'] ?? null;

    foreach ($data->getBody()['value'] as $key => $val) {
      $users[] = ['email' => strtolower($val['mail']), 'name' => $val['displayName']];
    }

    if($next && $next !== ''){
      $users = array_merge($users,($this->getGroupUsers("0",$next)));
    }

    return $users;
}

Inside my class I have functions to handle the token, I have this:

 private function _token(){

  try {

    $requestParms = ['form_params' => [
                      'client_id' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx',
                      'client_secret' => ':-)R|;F?HZ|Sk_}z@',
                      'resource' => 'https://graph.microsoft.com/',
                      'grant_type' => 'client_credentials',
                    ]];

    $requestURL = "https://login.microsoftonline.com/e660f793-4f40-4860-9845-5f588dfa220f/oauth2/token?api-version=1.0";

    $client = new GuzzleRetryClass();
    $response = $client->test($requestParms, $requestURL, ['timeout' => 3]);

    if (isset($response["access_token"])) {
      return $response["access_token"];
    } else {
      return false;
    }

  }
    catch (\Exception $e) {
          \Yii::error(print_r($e->getMessage(), true));
          return false;
  }

 }


 public function getToken(){
   return $this->_token();
 }

Then I can use my custom class like this:

$groupMembers = new MyCustomClass();
$members = $groupMembers->getGroupUsers("3e465b74-9460-4ac1-bbd6-79cb59ba521d"); // The ID of the group