protected function getToken($grantType, $resource, $refreshToken = null, $authorizationCode = null)
{
$token = new \stdClass();
$token->token = '';
$token->expireDate = '';
$token->error = '';
$path = '/' . $this->tenantName . '/oauth2/token';
$postFields = ['client_id' => $this->appClientId, 'client_secret' => $this->appSecretKey, 'grant_type' => $grantType, 'resource' => $resource];
if ($refreshToken) {
$postFields['refresh_token'] = $refreshToken;
}
if ($authorizationCode) {
$postFields['code'] = $authorizationCode;
}
$request = $this->getClient()->prepareRequest($path, 'POST', self::AUTH_API_VERSION, self::URL_LOGIN_WINDOWS);
$request->append($postFields);
$response = $this->getClient()->call($request);
$responseObj = json_decode($response->getContent());
if (empty($responseObj)) {
throw new AzureException('Bad server response format for token-request.', 400);
}
if (isset($responseObj->error)) {
throw new RestApiException($responseObj->error_description);
} else {
if (isset($responseObj->access_token)) {
$token->token = $responseObj->access_token;
} else {
if (isset($responseObj->token)) {
$token->token = $responseObj->token;
}
}
$token->expireDate = $responseObj->expires_on;
if (isset($responseObj->refresh_token)) {
$this->refreshToken = new \stdClass();
$this->refreshToken->token = $responseObj->refresh_token;
$this->refreshToken->expireDate = time() + 14 * 24 * 60 * 60;
$this->environment->keychain(\SERVER_PLATFORMS::AZURE)->properties->saveSettings([CloudCredentialsProperty::AZURE_REFRESH_TOKEN => $this->refreshToken->token, CloudCredentialsProperty::AZURE_REFRESH_TOKEN_EXPIRE => $this->refreshToken->expireDate]);
}
}
return $token;
}