Page 1 of 1

API Login Error - OpenCart 3.0.2.0

Posted: Mon Jul 24, 2017 11:26 am
by dinpsnl
function do_curl_request($url, $params=array()) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/apicookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/apicookie.txt');

$params_string = '';
if (is_array($params) && count($params)) {
foreach($params as $key=>$value) {
$params_string .= $key.'='.$value.'&';
}
rtrim($params_string, '&');

curl_setopt($ch,CURLOPT_POST, count($params));
curl_setopt($ch,CURLOPT_POSTFIELDS, $params_string);
}

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

return $result;
}

$url = 'http://localhost/opencart/index.php?route=api/login';
$fields = array(
'username' => 'newton',
'key' => 'GWHdoHsQ4I1jxQ2SUv6FYu626OZrtivQ8nN1tycm5ld4x6CLoFsk5wpRKv60Y5vTk1gbqg8howhYlcKW5fkBOcF12spVvKR0fVP0PIoOM6GUnFJLwsqEWxAo37mShU0lPfQU53EWDFfRoKibOqq5KFaFSCILPm7x4UsFBLUp0gMhgRT6Rc4IS3kiHMParqIEETmriMIF7xg5uJ1F2UK4PViKAzqiH8gNFCAWdnn8botZGjRUxsAEdUxLTttlmWXf',
);

$response = do_curl_request($url, $fields);
var_dump($response);

---------------------------
Here is my response


string(653) "Notice: Undefined index: api_token in /Applications/MAMP/htdocs/opencart/catalog/controller/startup/session.php on line 8Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/opencart/system/framework.php:42) in /Applications/MAMP/htdocs/opencart/catalog/controller/startup/startup.php on line 99Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/opencart/system/framework.php:42) in /Applications/MAMP/htdocs/opencart/catalog/controller/startup/startup.php on line 157"



What is the issue ? I tried adding 'api_token=1234' to my url then the error goes but still not logging in

Php error log

[24-Jul-2017 03:20:59 UTC] PHP Fatal error: Uncaught exception 'Exception' with message 'Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`key` = 'GWHdoHsQ4I1jxQ2SUv6FYu626OZrtivQ8nN1tycm5ld4x6CLoFsk5wpRKv60Y5vTk1gbqg8' at line 1<br />Error No: 1064<br />SELECT * FROM `oc_api` WHERE `username` = 'newton' `key` = 'GWHdoHsQ4I1jxQ2SUv6FYu626OZrtivQ8nN1tycm5ld4x6CLoFsk5wpRKv60Y5vTk1gbqg8howhYlcKW5fkBOcF12spVvKR0fVP0PIoOM6GUnFJLwsqEWxAo37mShU0lPfQU53EWDFfRoKibOqq5KFaFSCILPm7x4UsFBLUp0gMhgRT6Rc4IS3kiHMParqIEETmriMIF7xg5uJ1F2UK4PViKAzqiH8gNFCAWdnn8botZGjRUxsAEdUxLTttlmWXf' AND status = '1'' in /Applications/MAMP/htdocs/opencart/system/library/db/mysqli.php:40
Stack trace:
#0 /Applications/MAMP/htdocs/opencart/system/library/db.php(45): DB\MySQLi->query('SELECT * FROM `...')
#1 /Applications/MAMP/htdocs/opencart/catalog/model/account/api.php(4): DB->query('SELECT * FROM `...')
#2 [internal function]: ModelAccountApi->login('newton', 'GWHdoHsQ4I1jxQ2...')
#3 /Applicatio in /Applications/MAMP/htdocs/opencart/system/library/db/mysqli.php on line 40

Re: API Login Error - OpenCart 3.0.2.0

Posted: Sat Aug 19, 2017 12:30 am
by vimalmultimedia
Hi.... I am facing the same issue. anyone has the solution?

Re: API Login Error - OpenCart 3.0.2.0

Posted: Fri Aug 25, 2017 11:55 am
by boicute.14
Me too, im try more times, change method post to get ...
I think Opencart api its not complete, becase we had an SQL error and im found this.

Code: Select all

$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE `username` = '" . $this->db->escape($username) . "' `key` = '" . $this->db->escape($key) . "' AND status = '1'");
row 4 in file catalog/model/account/api.php and replace by

Code: Select all

$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE `username` = '" . $this->db->escape($username) . "' AND `key` = '" . $this->db->escape($key) . "' AND status = '1'");
my API login is work.

Re: API Login Error - OpenCart 3.0.2.0

Posted: Wed Sep 06, 2017 1:39 pm
by tdhungit
I found 2 error in OpenCart code
  1. In Model: catalog/model/account/api.php --> function login()

Code: Select all

public function login($username, $key) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE `username` = '" . $this->db->escape($username) . "' `key` = '" . $this->db->escape($key) . "' AND status = '1'");

		return $query->row;
	}
Fixed: change it as:

Code: Select all

public function login($username, $key) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE `username` = '" . $this->db->escape($username) . "' AND `key` = '" . $this->db->escape($key) . "' AND status = '1'");

		return $query->row;
	}
  • In Controller: catalog/controller/api/login.php
Line 30

Code: Select all

$session = new Session($config->get('session_engine'), $registry);
Fixed: change it as:

Code: Select all

$session = new Session($this->config->get('session_engine'), $this->registry);
It's work

Re: API Login Error - OpenCart 3.0.2.0

Posted: Wed Apr 25, 2018 5:59 pm
by Storeship
Thanks That worked, cant believe this untested code is default?

Re: API Login Error - OpenCart 3.0.2.0

Posted: Mon Aug 20, 2018 5:19 pm
by optiUser01
I made the above changes but it is still showing error:
<b>Notice</b>: Undefined index: api_token in
<b>/home/optimag5/public_html/opticart.tk/catalog/controller/startup/session.php</b> on line
<b>8</b>
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home/optimag5/public_html/opticart.tk/system/framework.php:42) in
<b>/home/optimag5/public_html/opticart.tk/catalog/controller/startup/startup.php</b> on line
<b>99</b>
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home/optimag5/public_html/opticart.tk/system/framework.php:42) in
<b>/home/optimag5/public_html/opticart.tk/catalog/controller/startup/startup.php</b> on line
<b>157</b>[]

Re: API Login Error - OpenCart 3.0.2.0

Posted: Thu Apr 04, 2019 9:02 pm
by emma2
hello
am a newbie with open cart 3.2.0, i want to use the apis set in open cart have added the default apis name and generated the key,and also selected it from the store under options tab, but its not setting the api session token and this is the error an getting
"<b>Notice</b>: Undefined index: api_token in <b>C:\xampp\htdocs\app1\catalog\controller\startup\session.php</b> on line <b>8</b><b>Notice</b>: Undefined index: username in <b>C:\xampp\htdocs\app1\catalog\controller\api\login.php</b> on line <b>11</b><b>Notice</b>: Undefined index: key in <b>C:\xampp\htdocs\app1\catalog\controller\api\login.php</b> on line <b>11</b>[]"

would someone help me

Re: API Login Error - OpenCart 3.0.2.0

Posted: Wed Oct 16, 2019 4:09 pm
by prosenjeet123
emma2 wrote:
Thu Apr 04, 2019 9:02 pm
hello
am a newbie with open cart 3.2.0, i want to use the apis set in open cart have added the default apis name and generated the key,and also selected it from the store under options tab, but its not setting the api session token and this is the error an getting
"<b>Notice</b>: Undefined index: api_token in <b>C:\xampp\htdocs\app1\catalog\controller\startup\session.php</b> on line <b>8</b><b>Notice</b>: Undefined index: username in <b>C:\xampp\htdocs\app1\catalog\controller\api\login.php</b> on line <b>11</b><b>Notice</b>: Undefined index: key in <b>C:\xampp\htdocs\app1\catalog\controller\api\login.php</b> on line <b>11</b>[]"

would someone help me
I have the same issue i did a complete fresh install and i am getting the same error on 3.0.3.2
Error:
<b>Notice</b>: Undefined index: key in <b>/catalog/controller/api/login.php</b> on line <b>14</b>[]

Re: API Login Error - OpenCart 3.0.2.0

Posted: Wed Oct 16, 2019 4:32 pm
by thekrotek
prosenjeet123 wrote:
Wed Oct 16, 2019 4:09 pm
I have the same issue i did a complete fresh install and i am getting the same error on 3.0.3.2
Error:
<b>Notice</b>: Undefined index: key in <b>/catalog/controller/api/login.php</b> on line <b>14</b>[]
Nobody will tell you remotely, what is wrong. The only way to sole this is to ask for commercial support.

Re: API Login Error - OpenCart 3.0.2.0

Posted: Wed Oct 16, 2019 5:37 pm
by letxobnav
Better check your curl stuff as these errors mean that you have no valid post variables for username and/or key and therefore have no api_token as well.