Page 1 of 1

route=api/login doesn't work

Posted: Wed May 08, 2019 2:29 am
by IgorJ
Hello,
Hello, I need help to login to my client oc store api. I ve done everything according to the docs (found here http://docs.opencart.com/en-gb/system/users/api/), but when I try to login in order to fetch the token, I get this response

"<b>Notice</b>: Undefined index: api_token in
<b>/var/app/current/public/catalog/controller/startup/session.php</b> on line
<b>8</b>"

I whitelisted my IP, I tripple checked my key and username, and really cant tell what is wrong with this.

here is my python code

Code: Select all

base_url = "https://www.terracesmenswear.co.uk/index.php"
login_route = "?route=api/login"

orders = "?route=api/order/history"
r = requests.post(base_url+login_route, data={
         'username':username,
         'key':key
     })
 print (r.text) 
     
this code outputs the mentined response
<b>Notice</b>: Undefined index: api_token in <b>/var/app/current/public/catalog/controller/startup/session.php</b> on line<b>8</b>

Re: route=api/login doesn't work

Posted: Wed May 08, 2019 5:11 am
by straightlight
No OC version posted.

Re: route=api/login doesn't work

Posted: Wed May 08, 2019 5:18 am
by thekrotek
OpenCart documentation is always outdated, to say the least. The best way is to look in the code and see, how login actually works, which data it passes and when. I hope you can read PHP, not just Python code.

Re: route=api/login doesn't work

Posted: Wed May 08, 2019 5:22 am
by straightlight
I have already reported the issue on GitHub regarding the api_token key in the admin order TWIG file returning the wrong array key from JSON response sometime ago: https://github.com/opencart/opencart/issues/7327 . However, if you want to use external API, I would suggest to use this method instead: viewtopic.php?f=202&t=203512&p=720446#p720446

Re: route=api/login doesn't work

Posted: Wed May 08, 2019 5:26 am
by straightlight
Also, if you're using Amazon, see this fix solution with api/login: https://github.com/opencart/opencart/pu ... 7aafdcf819 .

Re: route=api/login doesn't work

Posted: Wed May 08, 2019 5:29 am
by straightlight
I can no longer find the api_token commit on GitHub but also ensure that admin/view/template/sale/order_info.twig file:

Code: Select all

if (json['token']) {
      api_token = json['token'];
    }
is replaced with:

Code: Select all

if (json['api_token']) {
      api_token = json['api_token'];
    }
This should resolved the issue after following all the related links I posted. However, take note that this particular fix has been removed from the master branch on GitHub already.

Then, follow this FAQ: viewtopic.php?f=134&t=215776#p718325 .

Edit: Found the commit here: https://github.com/opencart/opencart/is ... -508942954 .

Re: route=api/login doesn't work

Posted: Wed May 08, 2019 5:38 am
by straightlight
Issue found with the key variable originating from admin/view/template/sale/order_info.twig file. However, this issue was already reported.

In admin/controller/sale/order.php file,

find:

Code: Select all

$this->response->setOutput($this->load->view('sale/order_info', $data));
A few lines up, find:

Code: Select all

$data['api_token'] = $session->getId();
add below:

Code: Select all

$data['api_key'] = $api_info['key'];
Then, below, find:

Code: Select all

$data['api_token'] = '';
add right below:

Code: Select all

$data['api_key'] = '';
This should fix the issue among the previous posts instructions above.

Note: This key POST is no longer showing in the master branch on GitHub.

Re: route=api/login doesn't work

Posted: Wed May 08, 2019 8:54 pm
by IgorJ
Thx everyone with their help, it was all very helpfull. Will need to consult with the project owner regarding the next step.
Cheers

Re: route=api/login doesn't work

Posted: Sat Dec 19, 2020 10:42 pm
by kerim92
hi @straightlight is it possible to use your github repo on lastest version of opencart? i mean 3.0.3.6
https://github.com/opencart/opencart/issues/7327

thanks in advance

Re: route=api/login doesn't work

Posted: Sun Dec 20, 2020 1:29 pm
by straightlight
As per Daniel, the API is still going to be used but in a different way. Rather focus on the master branch on how the API is now handled. The only problematic between the master and previous releases are the session handling that won't be too easy to mitigate on each other and that is if one or many people are able to integrate those changes without breaking their stores.