It seems to be a problem with the User Role.
To get started with the REST API in Magento 2 using token-based authentication, you will need to create a web service User Role and register that role to a new Magento 2 Admin User.
- Login to the Magento 2 Admin Panel.
- Go to System > User Roles and tap the Add New Role
- Enter the Role Name.
- In Your Password field, enter the current password of your Magento 2
Admin.
- Now, on the left side, click Role Resources.
- In the Resource Access, select only those that are required for your
web service. (in your case, products should be selected)
- Once done, hit the Save Role
Now, create a new user for the newly created role through these steps:
- Go to System > All Users and hit the Add New User
- Enter the required information including User Name, First and Last
Name, Email, Password, etc.
- Now, on the left side, click User Role and select the newly created
Role.
- Once done, click the Save User
Now, I will pass the newly created username and password with the API URL in the initial connection and receive the token. This token will be saved in a variable, which will be passed in the header for further calls.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
$token = curl_exec($ch);
//decoding generated token and saving it in a variable
$token= json_decode($token);
?>
You can fetch almost everything using Magento 2 REST API. In this answer, I will be fetching All Modules, but you can check out the List of REST APIs for Magento Open Source and see what you needed.
Now, I will pass the token (fetched earlier) in the header with the API URL to get all the modules installed on Magento 2 store.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
$token = curl_exec($ch);
//decoding generated token and saving it in a variable
$token= json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result= json_decode($result);
//printing result
print_r($result);
?>
Once you execute the above code (with relevant information), run the file in the browser and you will see output similar to the following:

If you need further help, check this complete guide: https://www.cloudways.com/blog/magento-2-rest-api/