cancel
Showing results for 
Search instead for 
Did you mean: 

Orders REST API issue

Orders REST API issue

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:

 

Consumer is not authorized to access %resources Magento_Sales::sales
 

Or returns a 1

 

What am I doing wrong in my script and why can't I access Orders with my admin account?

30 REPLIES 30

Re: Orders REST API issue

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 !

 

  

if issue solved,Click Kudos & Accept as Solution

Re: Orders REST API issue

Hello @trey_sinnis

 

I tried using postman with admin token for https://magento/index.php/rest/V1/orders/items URL then getting below error:Screenshot from 2018-10-02 01-03-33.png  1366×768 .png

After that added ?searchCriteria[currentPage]=1 param in url like: rest/V1/orders/items?searchCriteria[currentPage]=1

then api run successfully

Screenshot from 2018-10-02 01-04-45.png  1366×768 .png

 

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>';

 

Manish Mittal
https://www.manishmittal.com/

Re: Orders REST API issue

This was my original script before changing it to /items - didn't seem to work sorry :/

Re: Orders REST API issue

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 :/

Re: Orders REST API issue

Hi @trey_sinnis

 

Does it mean the scripts we have posted above its also not working ?

if issue solved,Click Kudos & Accept as Solution

Re: Orders REST API issue

Hey @Manthan Dave,

 

yeah nada is working :/

Re: Orders REST API issue

Hello @trey_sinnis

 

Please try 

$ch = curl_init('https://magento/index.php/rest/V1/orders/items?searchCriteria[currentPage]=1');
?searchCriteria[currentPage]=

 

Manish Mittal
https://www.manishmittal.com/

Re: Orders REST API issue

Same issue. I'm pretty sure it's at a modular-level - I can access other API endpoints, just not this one

 

Re: Orders REST API issue

@trey_sinnis

 

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? 

Manish Mittal
https://www.manishmittal.com/