Skip to Content
Menu
This question has been flagged
2 Replies
23383 Views

Hello,

I am trying to access /web/session/authenticate using python as:

>>> import requests
>>> import json
>>> url = "http://localhost:8089/web/session/authenticate"
>>> headers = {'Content-Type':'application/json'}
>>> data = json.dumps({"params":{"db":"test","login":"admin","password":"admin"}})
>>> r = requests.post(url=url,data=data,headers=headers)

but it gives me following error:

@page { margin: 2cm } p { margin-bottom: 0.25cm; line-height: 115% }

b'{"jsonrpc": "2.0", "id": null, "error": {"code": 404, "message": "404: Not Found", "data": {"name": "werkzeug.exceptions.NotFound", "debug": "Traceback (most recent call last):\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/odoo/http.py\\", line 656, in _handle_exception\\n return super(JsonRequest, self)._handle_exception(exception)\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/odoo/http.py\\", line 314, in _handle_exception\\n raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/odoo/tools/pycompat.py\\", line 87, in reraise\\n raise value\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/odoo/http.py\\", line 1460, in _dispatch_nodb\\n func, arguments = self.nodb_routing_map.bind_to_environ(request.httprequest.environ).match()\\n File \\"/usr/local/lib/python3.6/dist-packages/werkzeug/routing.py\\", line 1563, in match\\n raise NotFound()\\nwerkzeug.exceptions.NotFound: 404: Not Found\\n", "message": "404: Not Found", "arguments": [], "exception_type": "internal_error"}, "http_status": 404}}

After changing config file as dbfilter = test gives me following error:

b'{"jsonrpc": "2.0", "id": null, "error": {"code": 100, "message": "Odoo Session Expired", "data": {"name": "odoo.http.SessionExpiredException", "debug": "Traceback (most recent call last):\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/odoo/http.py\\", line 656, in _handle_exception\\n return super(JsonRequest, self)._handle_exception(exception)\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/odoo/http.py\\", line 314, in _handle_exception\\n raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/odoo/tools/pycompat.py\\", line 87, in reraise\\n raise value\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/addons/http_routing/models/ir_http.py\\", line 346, in _dispatch\\n cls._authenticate(func.routing[\'auth\'])\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/odoo/addons/base/models/ir_http.py\\", line 118, in _authenticate\\n getattr(cls, \\"_auth_method_%s\\" % auth_method)()\\n File \\"/usr/local/sampada/eclipse-workspace/odoo12/odoo/addons/base/models/ir_http.py\\", line 91, in _auth_method_user\\n raise http.SessionExpiredException(\\"Session expired\\")\\nodoo.http.SessionExpiredException: Session expired\\n", "message": "Session expired", "arguments": ["Session expired"], "exception_type": "internal_error"}}}

after clearing history, cookies and destroying sessions of user still same error occur


 
Avatar
Discard
Best Answer

You need to get the session id and pass it as cookie

here is the code example

parameter = {

            "db": "dbname",

            "login": "admin@gmail.com",

            "password": "admin",    

                        }

 

headers = {'Content-type': 'application/json'}      

AUTH_URL = "http://127.0.0.1:8072/web/session/authenticate/"

data = {

        "jsonrpc": "2.0",

        "params": {

                "login": parameter['login'],

                "password": parameter['password'],

                "db": parameter['db']

            }

        }  

res = rq.post(AUTH_URL, data=json.dumps(data),headers=headers)

session_id = res.cookies["session_id"]

base_url = "127.0.0.1:8072/api/v1/modules/" # your api 

json_data = json.dumps(parameter)

json_headers = {

    "Content-Type": "application/json",

    "Accept": "application/json",

    }

'''cookies = {

    'login': "admin@gmail.com", 

    'password': 'admin',

    'session_id':session_id

    }'''

req = rq.post("http://{}".format(base_url), data = json_data, headers=json_headers, cookies=cookies)

print(req.content) 

Avatar
Discard
Best Answer

Hi 
It seems everything is ok and it should work. 
but as error point out "Session expired" it should only occur in case of auth='user' on the route. 
however 'web/session/authenticate' route is auth="none" type so are you sure it's not overridden by another module (it change auth none to user)?

Avatar
Discard
Author

Yes I have change auth=none to auth=user. For auth=none work fine but I want authenticate user that is reason I am changing auth=user.how to authenticate user if auth=user?

auth="user" mean Http layer first check the user is logged in (it has a valid session-id) while auth="none" means it doesn't' check user authentication.

use of route /web/session/authenticate who have user credential can request this route and get a session which used for further communion. so this route must be auth="none" otherwise there is no need to authenticate if a user already login

Related Posts Replies Views Activity
3
Dec 22
2629
0
Nov 22
1907
1
Oct 20
1895
2
Apr 20
3558
1
Apr 20
2413