1

Get the an order through rest api

Here is the script i tried

<?php

    $userData = array("username" => "admin", "password" => "Abcd1234");
    $ch = curl_init("http://example.com/index.php/rest/all/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);
    var_dump($token);

    //$data = array("id"=>1);
    $ch = curl_init("http://example.com/index.php/rest/V1/orders/000000021");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    //curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));


$token = curl_exec($ch);

var_dump($token);

//var_dump(json_encode($data));

error message:

{"message":"The consumer isn't authorized to access %resources.","parameters":{"resources":"Magento_Sales::sales"}

the admin already has "ALL" resources, i don't know why the error message will show that.

6
  • Have you checked your password? May 30, 2019 at 10:31
  • because i have checked your code and it's returning perfect data May 30, 2019 at 10:32
  • @RutveeSojitra are you using version 2.3?
    – hkguile
    May 30, 2019 at 12:24
  • yes Magento 2.3.1 May 30, 2019 at 12:27
  • thanks, i just checked to use another website, it works. but i don't know why it didn't work for the original one
    – hkguile
    May 30, 2019 at 12:37

1 Answer 1

0

Try changing the PHP handler used by Apache on the Magento server. I changed from cgi to suphp and the requests started to work. Interestingly, as mentioned in https://stackoverflow.com/questions/57028498/problem-with-magento-2-rest-authentication/59530132, POST requests would always work, but GET requests would fail. I have no idea why changing the handler solves the problem. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.