System Center Operations Manager (SCOM) 1801 REST API PHP 中的身份验证

System Center Operations Manager (SCOM) 1801 REST API Authentication in PHP

我正在 PHP 中开发集成 class 以使用新的 REST API 接口访问 SCOM 数据。我找不到使用 PHP 连接到 API 的其他文档。我到目前为止是这样的:

$url = "http://scomserver/OperationsManager/authenticate";
$username = 'myuser';
$password = 'myPassw0rd';
$request = "";
$cookie = tempnam("/tmp", "CURLCOOKIE");
$requestHeaders = array('Content-Type, application/json; charset=utf-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, implode("\r\n", $requestHeaders));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, count($request));
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, -1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$buffer = curl_exec($ch);
$response = curl_getinfo( $ch );
curl_close($ch);

使用此代码我收到以下错误:

{"errorMessage":"Passed parameter cannot be null","errorTrace":""}

并且 IIS 日志报告 400 错误。

我该如何解决这个问题?有什么我想念的吗?

谢谢,
恩佐

您缺少请求正文。使用 NTLM 身份验证时,请求正文应通知 SCOM API 您正在这样做。为此,提交 "Windows" 作为 base-64 编码的 JSON 正文,它在您的代码中为空。参考这个例子(它在 PowerShell 中,但应该是可读的):https://community.squaredup.com/answers/question/scom-1801-rest-api/.

换句话说,不要更改您的 PHP 代码,而是:

$request = "Windows";
$request = utf8_encode($request);
$request = "\"".base64_encode($request)."\""; // should give you ["V2luZG93cw=="]