Post by albertusgeyser » Sat Nov 24, 2018 3:19 pm

Good Day

I tried to use login code with python via api and it gives me following error:

Notice: Undefined index: api_token in /var/www/html/opencart/catalog/controller/startup/session.php on line 8Notice: Undefined index: username in /var/www/html/opencart/catalog/controller/api/login.php on line 11Notice: Undefined index: key in /var/www/html/opencart/catalog/controller/api/login.php on line 11

What changes should i make?

Code: Select all

import requests

s = requests.Session()

username = 'Default'
key='43U3dt1zhgpX7Qgt8hKIDzDhbRFeMmxFkijgi6MymABWT8HX6lw0zOM2aTfAfx3lPpbhDaTZsdv47PXNvb7f8CBK7lvs3G1di4GnMEj777ysWVZwdKNlx6pZ70cV0s4qZAtqBgMr5QgxQkkNFnngoNHpEtWN0lZJTGGLvs7kAabXSTywMbE7DeO7rbQLB3xXGEyq4MywRicRJA3arNOKMpBWHEoXY4KzyiDqymjmn0QJ66vXAxsssG5TN1mtaLra'
# Actually, key is 256 character-long

msg= s.post(
    'http://domain.co.za/opencart/index.php?route=api/login',
    data={'username':username, 'key':key}
).text

print(msg)


Posts

Joined
Tue Jan 23, 2018 4:17 am

Post by straightlight » Sun Nov 25, 2018 5:38 am

The username and the key variables are defined but not the api_token in your query.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by albertusgeyser » Sun Nov 25, 2018 5:51 am

Good Day

Thank you for reply, but is this not part that should return api_token?
I found this code at the following link:
http://docs.opencart.com/en-gb/system/users/api/

Hope someone could help.

Thank You

Albertus Geyser


Posts

Joined
Tue Jan 23, 2018 4:17 am

Post by albertusgeyser » Sun Nov 25, 2018 2:03 pm

Good Day

I checked more and found this:
viewtopic.php?t=204392

Still do not understand why and how should you submit api_token if that is what you should get when submitting login?

Hope someone could guide me to correct this?

Thank You

Albertus Geyser


Posts

Joined
Tue Jan 23, 2018 4:17 am

Post by straightlight » Sun Nov 25, 2018 8:32 pm

An example you could use would be in your admin/controller/sale/order.php file where it initiates the API token key. Check for: new Session . However, since you are enquiring about a python code, you'd need to convert the instantiation to python or you'd had to capture the created api_token key's session super globals over python.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by albertusgeyser » Sun Nov 25, 2018 11:04 pm

Good Day

Thank you for reply.

Is there someone with python code that could help me please?

Thank You

Albertus Geyser


Posts

Joined
Tue Jan 23, 2018 4:17 am

Post by straightlight » Mon Nov 26, 2018 1:26 am

An example for the session super globals: https://www.programcreek.com/python/exa ... ts.Session . You could also use the already implemented Ajax request solution to pass the api_id to the URL with the help of the method: getURLVar() or as a POST request (more recommended than GET in this case) from your catalog's javascript folder. Or, you could create a new service request in the Commercial Support section of the forum to get this done as a custom job.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by albertusgeyser » Mon Nov 26, 2018 3:10 am

Good Day

Thank you for your support. I "unfortunately" have to use python as software i want to integrate with is written in python.
Will see if i will manage to get it going otherwise have to look for alternative ways of doing this.

Thank You

Albertus Geyser


Posts

Joined
Tue Jan 23, 2018 4:17 am

Post by albertusgeyser » Mon Nov 26, 2018 5:37 am

Good Day

I managed to get Open Cart sessionId (api_token), but still struggle to login:

Code: Select all

import requests

username = 'Default'
key = '4tU3dt1zhgpX7QGt8hKIDzDhbRFeMmUFkijgi6nymABWT8HX6lw0zOM2aTfAfx3lPpbhDaTZsdv47PXNvb7g8CsK7lvs3G1Ci4GnMEj777ysWvZwdKNlx6pZ70cV0s4qZAtqBgsr5QgxQkkNFnngoNHpEtwN0lZJTGGLvs7kAabXsTywMbE7DeO7rBQLB3xdGEyq4MywrHcRJA3arNOKMpBWHEoXY4azyiCqymomn0QJ66vmAxsssG5SN1ataLrX'

session = requests.session()
response = session.get("http://domain.co.za/opencart/index.php?route=api/login")
cookies = response.cookies.get_dict()
sessionId = cookies['OCSESSID']

print(sessionId)

session.post(
    'http://domain.co.za/opencart/index.php?route=api/login',
  params={'api_token': sessionId},
  data={'username':username, 'key':key}
)

datax = session.post(
    'http://domain.co.za/opencart/index.php?route=api/order/info',
  params={'api_token': sessionId},
  data={}
).text

print(datax)
Is sessionId and api_token same thing?

Any Help Please.

Thank You

Albertus Geyser


Posts

Joined
Tue Jan 23, 2018 4:17 am

Post by straightlight » Tue Nov 27, 2018 6:47 am

Using cookies for APIs are unsecured with session super globals. Not in good practice with APIs.

Code: Select all

params={'api_token': sessionId}
to:

Code: Select all

params={'api_id': sessionId}

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by albertusgeyser » Sun Dec 30, 2018 7:19 pm

Good Day

Api Documentation: http://docs.opencart.com/en-gb/system/users/api/
Show how it should be done, but still i cant get api to work.
I desperately want to use opencart, but need api to work.

Anyone that could help in this regard?

Thank You

Albertus Geyser


Posts

Joined
Tue Jan 23, 2018 4:17 am
Who is online

Users browsing this forum: No registered users and 13 guests