I am getting the following error while updating the all profile information of customer using its ID.
Error:
Array
(
[message] => Consumer is not authorized to access %resources
[parameters] => Array
(
[resources] => Magento_Customer::manage
)
[trace] => #0
)
I am explaining my code below.
<?php
$response=array();
$adminUrl='http://magento-148486-425775.cloudwaysapps.com/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "rajesh", "password" => "Abcd@987.");
$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);
$email=$_POST['email'];
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$gender=$_POST['gender'];
$dob=$_POST['dob'];
$user_id=$_POST['user_id'];
$isPasswordChange=$_POST['isPasswordChange'];
$mobile=$_POST['mobile'];
if ($isPasswordChange==1) {
$oldPassword=$_POST['oldPassword'];
$newPassword=$_POST['newPassword'];
}
$sql=mysqli_query($conn,"select * from mobusers where id='".$user_id."'");
if (mysqli_num_rows($sql) > 0) {
$row=mysqli_fetch_array($sql);
$ent_id=$row['entity_id'];
if ($dob=='' && $gender=='') {
$customerData = [
'customer' => [
"id" => $ent_id,
"email" => $email,
"firstname" => $first_name,
"lastname" => $last_name,
"storeId" => 1,
"websiteId" => 1,
"customAttributes" => [
[
"attributeCode" => "mobile",
"value" => $mobile
]
],
],
];
}else if ($dob !='' && $gender=='') {
$customerData = [
'customer' => [
"id" => $ent_id,
"email" => $email,
"firstname" => $first_name,
"lastname" => $last_name,
"dob" => $dob,
"storeId" => 1,
"websiteId" => 1,
"customAttributes" => [
[
"attributeCode" => "mobile",
"value" => $mobile
]
],
],
];
}else if ($dob =='' && $gender !='') {
$customerData = [
'customer' => [
"id" => $ent_id,
"email" => $email,
"firstname" => $first_name,
"lastname" => $last_name,
"gender" => $gender,
"storeId" => 1,
"websiteId" => 1,
"customAttributes" => [
[
"attributeCode" => "mobile",
"value" => $mobile
]
],
],
];
}else{
$customerData = [
'customer' => [
"id" => $ent_id,
"email" => $email,
"firstname" => $first_name,
"lastname" => $last_name,
"gender" => $gender,
"dob" => $dob,
"storeId" => 1,
"websiteId" => 1,
"customAttributes" => [
[
"attributeCode" => "mobile",
"value" => $mobile
]
],
],
];
}
$headers = array("Authorization: Bearer $token");
$requestUrl='http://magento-148486-425775.cloudwaysapps.com/index.php/rest/V1/customers/'.$ent_id;
$ch = curl_init();
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
$result = curl_exec($ch);
$result = json_decode($result, 1);
print_r($result);exit;
if (!isset($result['message']) && $result > 0) {
$sql="UPDATE mobusers SET uemail='".$email."',mobile_no='".$mobile."' WHERE id='".$user_id."' ";
$sql=mysqli_query($conn,$sql);
if ($isPasswordChange==1) {
$customerData=array("currentPassword"=>$oldPassword,"newPassword"=>$newPassword);
$headers = array("Authorization: Bearer $token");
$requestUrl='http://magento-148486-425775.cloudwaysapps.com/index.php/rest/V1/customers/me/password?customerId='.$ent_id;
$ch = curl_init();
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
$result1 = curl_exec($ch);
$result1 = json_decode($result1, 1);
if (!isset($result1['message']) && $result1 > 0) {
$sql="UPDATE mobusers SET password='".$password."' WHERE id='".$user_id."' ";
$sql=mysqli_query($conn,$sql);
}
}
if ($sql) {
$response['status']="Success!";
$response['msg']="Your profile has been updated successfully.";
}
}
}else{
$response['status']="Error";
$response['msg']="Failed!";
}
echo json_encode($response);
Here I am sending data from postman
and trying to update all data as per customer id
. Means if isPasswordChange==1
then I am updating also the password but in my case I am getting those error. Please help me to resolve this issue and update all data as expected.
customers/me
in API.customer id
.