clientId = CLIENT_ID; $this->clientSecret = CLIENT_SECRET; $this->tenantId = TENANT_ID; $this->endpoint = "https://login.microsoftonline.com/{$this->tenantId}/oauth2/v2.0/token"; $this->user = TEST_USER_UPN; $this->getAuthenticatedClient(); } public function getAuthenticatedClient() { if ($this->graphClient == null) { $this->graphClient = new Graph(); $this->graphClient->setAccessToken($this->getAccessToken()); // $this->graphClient->setProxyPort("localhost:8888"); // Need for fiddler. } } public function getAccessToken() { $body = "grant_type=".$this->grantType ."&client_info=1" ."&client_id=".$this->clientId ."&scope=".$this->scopes ."&client_secret=".$this->clientSecret; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->endpoint); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_FAILONERROR, 0); // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // turns off SSL check, // curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:8888"); // need for fiddler + auth curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->contentType, 'Content-Length: ' . strlen($body))); $result = curl_exec ($ch); $token = json_decode($result, true)['access_token']; curl_close($ch); return $token; } }