Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
8 Antwoorden
21850 Weergaven

I am trying to create a custom controller with user authentication.
Here is what i did.

        class GetUserDetails(http.Controller):
        @http.route('/web/getUserDetail',auth='user',type='json')
        def getChit(self,**kw):
            print('Inside getUser detail',kw)

Issue 1: When i call this controller from an API client (ARC) its directly get into that function, without any authentication.???

Issue 2: If we solved the above issue, how can i authenticate a user via API call/client.

Actually i want to get user details in android app.

Avatar
Annuleer
Beste antwoord

Hi,

If you want to call a controller with auth='user' you should first get your user id and session_id and then continue from there. You can get the session_details from /web/session/authenticate like this:

session_details = requests.get(url=odoo_url + '/web/session/authenticate', data=json.dumps(data_string), headers=headers)
session_id = str(session_details.cookies.get('session_id'))

Once you have the session details you can set your cookies/values and call the endpoint:

cookies = {
'username': db_username,
'password': db_password,
'session_id': session_id // which we just got in the previous code block
}
requests.get(url=odoo_url + '/web/getUserDetail', params=params, headers=headers, cookies=cookies)

Regards,
Yenthe

Avatar
Annuleer
Auteur

@ Yenthe, i got the session id and i tried to call the url as per your solution but getting this error `Function declared as capable of handling request of type 'json' but called with a request of type 'http'`.

That happens when you call the controller with the wrong type or data. Make sure you're doing a get request which has the parameters, headers and cookies and that it contains JSON.

Is it available in Odoo 14?

we need to reassign user and pass in cookies as well ?? if we authenticate

Beste antwoord

To create a custom controller with user authentication in Odoo 13, define your controller class using @http.route and implement the @http.auth decorator for user access. Ensure to handle session management and access rights in your methods accordingly.

Avatar
Annuleer
Beste antwoord

As I was searching for Odoo user authentication via Postman and this thread was the best matching result I want to share resulting minimal example

Hopes it will help somebody

Environment: containerized Odoo17

import yaml

from odoo import http, Command
from odoo.http import request

class ExampleController(http.Controller):
@http.route('/api/v1/example/', type='json', auth='user', methods=['POST'], csrf=True)
def create_example(self):
kwargs = yaml.load(request.httprequest.data) # incoming json data
resp = some_internal_function(kwargs)
return resp


Authentication request in postman

http://127.0.0.1:8069/web/session/authenticate

Headers: Content-Type: application/json Body: (raw/JSON)

{
"params": {
"db": "my_odoo_dbname",
"login": "user",
"password": "their_password"
}
}

If succeed, in response there will be Cookies section. <>
Copy session_id​ key


Authenticated request in postman

http://127.0.0.1:8069/api/v1/example/

Headers: Content-Type: application/json/
Cookie: session_id=YOURSESSIONID Body: (raw/JSON)
YOUR JSON BODY
Avatar
Annuleer
Beste antwoord

I have the same problem now, looks like odoo removed session concept since some release.  So the web client session never expires, and there is no session in json-rpc interface at all, only user id is used there.  Did you guys use session id finally?

Avatar
Annuleer
Beste antwoord

default on odoo 13 there is no session_id response, but you can add it manual by add:

it from addons: 'Auth Session Info' by Hariprasath.B

# -*- coding: utf-8 -*-

from odoo import models
from odoo.http import request


class Http(models.AbstractModel):
_inherit = 'ir.http'

def session_info(self):
res = super(Http, self).session_info()
if not res.get('session_id'):
# Add Session Id
res['session_id'] = request.session.sid
return res
Avatar
Annuleer
Beste antwoord

@Niyas , the response is as you got. But if you try session_details.cookies.get('session_id') will get session_id definitely.

Avatar
Annuleer
Beste antwoord

In Odoo 13 the session_id is in cookies

Avatar
Annuleer
Beste antwoord

Hi,

As the auth='user' is given,  the route the controller will be accessible only for the authenticated users, else it will show some invalid or session_expired response .


Issue 1: I have checked some custom functions using the postman application and it seems working fine with auth='user' , if there is no valid session_id it will ask to authenticate first. In your case can you try the same with the postman application and confirm.

Issue 2: For authentication use the controller, /web/session/authenticate .


See the detailed Video Here: Authentication, Fetching Data & Creating Records Using Controller


Thanks

Avatar
Annuleer
Auteur

@Niyas, Thanks for the answer.

I tried to gt session by calling `http://localhost:5013/web/session/authenticate`. in the response, i couldn't find `sessino_id`.

here is the output.

{

"jsonrpc": "2.0",

"id": null,

"result": {

"uid": 2,

"is_system": true,

"is_admin": true,

"user_context": {

"lang": "en_US",

"tz": false,

"uid": 2

},

"db": "demo_odoo13",

"server_version": "13.0-20191007",

"server_version_info": [

13,

0,

0,

"final",

0,

""

],

"name": "Administrator",

"username": "admin",

"partner_display_name": "Administrator",

"company_id": 1,

"partner_id": 3,

"user_companies": {

"current_company": [

1,

"My Company"

],

"allowed_companies": [

[

1,

"My Company"

]

]

},

"currencies": {

"1": {

"symbol": "€",

"position": "after",

"digits": [

69,

2

]

},

"2": {

"symbol": "$",

"position": "before",

"digits": [

69,

2

]

}

},

"web.base.url": "http://localhost:5013",

"show_effect": "True",

"display_switch_company_menu": false,

"cache_hashes": {

"load_menus": "b7ea4d99293455ecb82c8d87efd5653792dc22b7",

"qweb": "befe30ca4d3deb2880b68d2532163699398dba61",

"translations": "026ae15487c2067877f7b82ffb991974c070d399"

},

"web_tours": [],

"out_of_office_message": false,

"odoobot_initialized": true

}

}

Auteur

@ Niyas, I tried with post man, but the same result, i can access it without session_id. When i print `request.env.user.name` it Prints `Administrator`.

@Niyas I have query related to api posted in the odoo forum please check this link https://www.odoo.com/forum/help-1/api-get-model-data-with-particular-user-only-190580

Gerelateerde posts Antwoorden Weergaven Activiteit
1
sep. 21
1783
4
jan. 19
3901
1
mei 17
4636
0
okt. 16
3625
1
aug. 15
6367