Get token for Rest API

I’m trying to use the rest API to bring data from an external Concrete CMS site in using PHP.

I’m using the following code to try and get a token:

$authurl = "https://www.katalysis.net/oauth/2.0/token";

$client_id = $site->getApiClientId();
$client_secret = $site->getApiClientSecret();

 // Creating base 64 encoded authkey
$Auth_Key = $client_id.":".$client_secret;
$encoded_Auth_Key=base64_encode($Auth_Key);

$headers = array();
$headers['Authorization'] = "Basic ".$encoded_Auth_Key;
$headers['Content-Type'] = "application/x-www-form-urlencoded";

$data = array(
    'grant_type' => 'client_credentials',
    'scope'      => 'system:info:read',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authurl);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, '1L');
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

$auth = curl_exec( $ch );

if ( curl_errno( $ch ) ){
    echo 'Error: ' . curl_error( $ch );
}
curl_close($ch);

$secret = json_decode($auth);
$access_key = $secret->access_token;

print_r($secret);

This is returning the error:

stdClass Object ( [error] => invalid_request [error_description] => The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. [hint] => Check the client_id parameter [message] => The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. )

Any idea what I’m doing wrong?

Solved this one - needed this:

 $data = array(
     'client_id'         =>  'ID',
     'client_secret'     => 'SECRET',
     'grant_type' => 'client_credentials',
     'scope'      => 'system:info:read',
);
1 Like