{"message":"Consumer is not authorized to access %resources","parameters":{"resources":"self"},"trace" ... Error in Addtocart rest api . please share solution .
thank you so much in advance
Please use below REST API Code to get all categories
$categoryId=$product_sku=$this->getRequest()->getParam('id');
$adminUrl='http://yourdomain/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "Raj", "password" => "mani123!!");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token= json_decode($token);
//exit();
//echo $token;
//Use above token into header
$headers = array("Authorization: Bearer $token");
if(isset($categoryId)){
$requestUrl = "http://18.219.227.98/index.php/rest/V1/categories/".$categoryId;
}else{
$requestUrl = "http://18.219.227.98/index.php/rest/V1/categories/";
}
$ch = curl_init();
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result= json_decode($result);
$data[]=array('error'=>'false','message'=>'Category List','status_code'=>'200','data'=>$result);
unset($data['username']);
unset($data['password']);
echo json_encode($data[0], JSON_UNESCAPED_SLASHES);
Please follow below code used for
magento 2 rest api admin token authentication Token Process
------------------------------------------------------------------------------------------
$categoryId=$product_sku=$this->getRequest()->getParam('id');
$adminUrl='http://yourdomain/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "Raj", "password" => "mani123!!");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token= json_decode($token);
//exit();
//echo $token;
//Use above token into header
$headers = array("Authorization: Bearer $token");
--------------------------------------------------------------------------------------
Below Code Used Magento 2 Rest API For All Categories Listing as JSON Format
if(isset($categoryId)){
$requestUrl = "http://yourdomain/index.php/rest/V1/categories/".$categoryId;
}else{
$requestUrl = "http://yourdomain/index.php/rest/V1/categories/";
}
$ch = curl_init();
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result= json_decode($result);
$data[]=array('error'=>'false','message'=>'Category List','status_code'=>'200','data'=>$result);
unset($data['username']);
unset($data['password']);
echo json_encode($data[0], JSON_UNESCAPED_SLASHES);
--------------------------------------------------------------------------------------
Please let me know if any other REST APU Issue
This issue related magento 2 rest api admin token authentication
There are two type magento 2 rest api token authentication
Admin Based another one Customer
Admin Based
---------------------------------------------------------------------------
$adminUrl='http://yourdomain/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "Raj", "password" => "mani123!!");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token= json_decode($token);
-------------------------------------------------------------------------------
Customer Based
$adminUrl='http://yourdomain/index.php/rest/V1/integration/customer/token';
$ch = curl_init();
$data = array("username" => "$cust_email", "password" => "$cust_currentpassword");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token= json_decode($token);
------------------------------------------------------
Your should as per application requirement