Using API with native PHP

Good day. There was a task to use API to work with CMS 3.0.0.

The development will be done with native PHP.

In CMS I created an application, got Client ID and Client Secret as described in 1.8 API Introduction
Not clear how to use this.
I can not find information on how to do authorize requests for the API.

If possible, show examples of code for sending API requests from PHP

I am trying to get accessToken as written here: 1.8 API Introduction

I must say right away that there are 2 errors in the example code:

  • we do not have “,” at the end of the line
    ‘responseType’ => “JSON_OR_STRING”

  • in line
    $ token = $ this → provider → getAccessToken (“authorizaton_code”, [

error, no need to use $ this-> here:
$ token = $ provider → getAccessToken (“authorizaton_code”, [

In any case, it is not possible to get an accessToken using this.

My attempt to get the token:

$url = “http://dev…/”;
$authPath = “api/authorize/access_token”;
$clientId = “30a5b2a…”;
$clientSecret = “64be54ed0…”;

$provider = new \Xibo\OAuth2\Client\Provider\Xibo([
‘sandbox’ => true,
‘clientId’ => $clientId,
‘clientSecret’ => $clientSecret,
‘responseType’ => ‘json’,
‘redirectUri’ => $url
]);

//$token = $provider->getAccessToken(‘refresh_token’, [
// ‘grant_type’ => ‘refresh_token’,
// ‘refresh_token’ => “REFRESH_TOKEN”
//]);

// OR (to get the token)
$token = $provider->getAccessToken(“authorizaton_code”, [
//‘code’ => $_GET[‘code’]
]);

print_r($token);

Please, tell me, what am I doing wrong?

( ! ) Fatal error: Uncaught League\OAuth2\Client\Grant\Exception\InvalidGrantException: Grant “League\OAuth2\Client\Grant\AuthorizatonCode” must extend AbstractGrant in C:\wamp\www\xibo\vendor\league\oauth2-client\src\Grant\GrantFactory.php on line 98
( ! ) League\OAuth2\Client\Grant\Exception\InvalidGrantException: Grant “League\OAuth2\Client\Grant\AuthorizatonCode” must extend AbstractGrant in \vendor\league\oauth2-client\src\Grant\GrantFactory.php on line 98

I am trying to create accessToken by example from
… \xibo\vendor\xibosignage\oauth2-xibo-cms\example\assignToLibrary.php

I get the error:
{“success”: false, “error”: 404, “message”: “404 Not Found”, “help”: “The requested resource could not be found. Please verify the URI and try again.”}

Postman also fails to get the token. The result is the same:
{
“success”: false,
“error”: 404,
“message”: “404 Not Found”,
“help”: “The requested resource could not be found. Please verify the URI and try again.”
}

The issue has been resolved.

I generated all three files with generators, assigned permissions 600 to the files. Finally, I was able to get access_token without errors.

An example of a PHP script that receives data from a playlist playlistId = 1:

<?php

$clientId = "ac3...";
$clientSecret = "5C0...";
$grant_type = "client_credentials";

$url = 'http://...';

$data = array(
    'client_id' => $clientId,
    'client_secret' => $clientSecret,
    'grant_type' => $grant_type
);

$data1 = array(
    'playlistId' => 1,
);

// Get access_token
$curl = curl_init($url . '/api/authorize/access_token');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($curl);
curl_close($curl);
$access_token = json_decode($response, true)['access_token'];
//print_r("token: " . $access_token);

// Get playlist 1 info
$curl1 = curl_init($url . '/api/playlist');
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($curl1, CURLOPT_POST, true);
// curl_setopt($curl1, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl1, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl1, CURLOPT_POSTFIELDS, json_encode($data1));
curl_setopt($curl1, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $access_token
));
$response = curl_exec($curl1);
curl_close($curl1);

var_dump(json_decode($response, true));