phprestscom

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


I am developing an integration class in PHP to access SCOM data using the new REST API interface. I cannot find additional documentation to connect to the API using PHP. What I have so far is this:

$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);

With this code I am getting the following error:

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

And IIS logs report 400 error.

How can I fix this? Is there something I am missing?

Thanks,
Enzo


Solution

  • You're missing request body. When use NTLM authentication, request body shoud notify SCOM API that you're doing that. For this, submit "Windows" as base-64 encoded JSON body, which is empty in your code. Refer to this example (it's in PowerShell, but should be readable): https://community.squaredup.com/answers/question/scom-1801-rest-api/.

    In other words, make not changes to your PHP code, but:

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