2

I'm new to Magento, and even newer to Magento 2, and am trying to get an output of all sales orders in json.

I followed the following tutorial: http://blog.i13websolution.com/magento-2-rest-api-example/

I can get this to work locally, however when I migrated to my stage server I get the following message:

You did not sign in correctly or your account is temporarily disabled

I've checked, and the account works, I can log into it without an issue via the admin backend. It's restricted to only being able to access the Sales Orders page and this seems to work fine in the backend.

I have also created a user role with the correct permissions identical to my local environment.

The code as it is set up is as follows:

$serverip = $_SERVER['REMOTE_ADDR'];
$adminUrl='http://'.$_SERVER['HTTP_HOST'].'/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
//Stage
$data = array("username" => "username", "password" => "password");

//Local
if($serverip == '127.0.0.1'){
$data = array("username" => "username2", "password" => "password23");
}

$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);

//Use above token into header
$headers = array("Authorization: Bearer $token");

//Call all pending values
$requestUrl='http://'.$_SERVER['HTTP_HOST'].'/index.php/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=status&searchCriteria[filter_groups][0][filters][0][value]=Pending';

$ch = curl_init();
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
print_r($result);

I'm quite stumped as to why the above isn't working on an actual server. Curl is installed, the user is set up... any pointers would be appreciated.

1
  • Had same problem with some accounts. Found no similar issue at magentocommerce.com. Workaround is to create yet another account. Reported as bug #1510 at Magento. Commented Aug 29, 2016 at 16:40

2 Answers 2

1

Resolved this issue.

The API would only accept a username without underscores in, may have been a server set up issue.

0

if you have a problem with TOKEN based authentication you can try OAUTH authentication it's easy to use.

go to system-->integration-->add new integration

it will generate four tokens . you can use these tokens to get authenticated to magento resources

1
  • mee to faced same authentication problem to API when started working on magento.that's why i moved to OAUTH based authentication.and download magento 2 developers guide in online it would be more usefull
    – vijay b
    Commented Aug 25, 2016 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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