Hey,
I have this script:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);
$usr = [
'username' => 'admin',
'password' => 'pass'
];
$ch = curl_init('http://magento/index.php/rest/V1/integration/admin/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($usr));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Content-Length: '. strlen(json_encode($usr))]);
$token = curl_exec($ch);
echo '<pre>';
print_r($token);
echo '</pre>';
echo '======================== <br />';
$ch = curl_init('https://magento/index.php/rest/V1/orders/items');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Authorization: Bearer '. json_decode($token)]);
$result = curl_exec($ch);
echo '<h1>Results</h1><hr />';
echo '<pre>'. print_r($result) .'</pre>';
And this is either returning:
Or returns a 1
What am I doing wrong in my script and why can't I access Orders with my admin account?
Hi @trey_sinnis
Looking at the code it seems like getting the admin token API and its code is perfect , but the error is coming from the order API.
Here you need to pass the order ID along with the accesstoken also you need to do amendments in the order API Url as well.
Below i am sharing the updated code :
$userData = array("username" => "admin", "password" => "pass"); $ch = curl_init("http://magento/index.php/rest/V1/integration/admin/token"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData)))); $token = curl_exec($ch); $ch = curl_init("https://magento/index.php/rest/V1/orders/1"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token))); $result = curl_exec($ch); $result = json_decode($result, 1); echo '<pre>';print_r($result);
Try the above scripts and let me know if you still face any issue on the same.
Hope it helps !
Hello @trey_sinnis
I tried using postman with admin token for https://magento/index.php/rest/V1/orders/items URL then getting below error:
After that added ?searchCriteria[currentPage]=1 param in url like: rest/V1/orders/items?searchCriteria[currentPage]=1
then api run successfully
Below sharing code is perfect, please check admin username and password is correct or not with proper permission.
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(-1); $usr = array("username" => "admin", "password" => "pass"); $ch = curl_init("http://magento/index.php/rest/V1/integration/admin/token"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Content-Length: '. strlen(json_encode($usr))]); $token = curl_exec($ch); $ch = curl_init('https://magento/index.php/rest/V1/orders/items?searchCriteria[currentPage]=1'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token))); $result = curl_exec($ch); echo '<h1>Results</h1><hr />'; echo '<pre>'. print_r($result) .'</pre>';
This was my original script before changing it to /items - didn't seem to work sorry :/
Amended script to your recommendation and it didn't work I'm afraid :/ not sure what's up, it's literally a vanilla installation with an admin account :/
Hi @trey_sinnis
Does it mean the scripts we have posted above its also not working ?
Hello @trey_sinnis
Please try
$ch = curl_init('https://magento/index.php/rest/V1/orders/items?searchCriteria[currentPage]=1');
?searchCriteria[currentPage]=
Same issue. I'm pretty sure it's at a modular-level - I can access other API endpoints, just not this one
I shared screenshots with you in my previous posts, I tried with postman and it is working properly.
Can you please check with postman once as I shared and also please let me know did you override this module?