Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Aplikacije
    Finance
    • Knjigovodstvo
    • Obračun
    • Stroški
    • Spreadsheet (BI)
    • Dokumenti
    • Podpisovanje
    Prodaja
    • CRM
    • Prodaja
    • POS Shop
    • POS Restaurant
    • Naročnine
    • Najem
    Spletne strani
    • Website Builder
    • Spletna trgovina
    • Blog
    • Forum
    • Pogovor v živo
    • eUčenje
    Dobavna veriga
    • Zaloga
    • Proizvodnja
    • PLM
    • Nabava
    • Vzdrževanje
    • Kakovost
    Kadri
    • Kadri
    • Kadrovanje
    • Odsotnost
    • Ocenjevanja
    • Priporočila
    • Vozni park
    Marketing
    • Družbeno Trženje
    • Email Marketing
    • SMS Marketing
    • Dogodki
    • Avtomatizacija trženja
    • Ankete
    Storitve
    • Projekt
    • Časovnice
    • Storitve na terenu
    • Služba za pomoč
    • Načrtovanje
    • Termini
    Produktivnost
    • Razprave
    • Odobritve
    • IoT
    • Voip
    • Znanje
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industrije
    Trgovina na drobno
    • Book Store
    • Trgovina z oblačili
    • Trgovina s pohištvom
    • Grocery Store
    • Trgovina s strojno opremo računalnikov
    • Trgovina z igračami
    Food & Hospitality
    • Bar and Pub
    • Restavracija
    • Hitra hrana
    • Guest House
    • Beverage Distributor
    • Hotel
    Nepremičnine
    • Real Estate Agency
    • Arhitekturno podjetje
    • Gradbeništvo
    • Estate Management
    • Vrtnarjenje
    • Združenje lastnikov nepremičnin
    Svetovanje
    • Računovodsko podjetje
    • Odoo Partner
    • Marketinška agencija
    • Law firm
    • Pridobivanje talentov
    • Audit & Certification
    Proizvodnja
    • Tekstil
    • Metal
    • Pohištvo
    • Hrana
    • Brewery
    • Poslovna darila
    Health & Fitness
    • Športni klub
    • Trgovina z očali
    • Fitnes center
    • Wellness Practitioners
    • Lekarna
    • Frizerski salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Sistemi sončne energije
    • Izdelovalec čevljev
    • Čistilne storitve
    • HVAC Services
    Ostali
    • Neprofitna organizacija
    • Agencija za okolje
    • Najem oglasnih panojev
    • Fotografija
    • Najem koles
    • Prodajalec programske opreme
    Browse all Industries
  • Skupnost
    Learn
    • Tutorials
    • Dokumentacija
    • Certifikati
    • Šolanje
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Prenesi
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Dogodki
    • Prevodi
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Sklici kupca
    • Podpora
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Določanje cen
  • Pomoč

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

How i can auth my user to access odoo with API Key or Access Token?

Naroči se

Get notified when there's activity on this post

This question has been flagged
authenticationapi
4 Odgovori
44138 Prikazi
Avatar
Usman Farzand

i'm using Odoo 14 CE
using addon
https://apps.odoo.com/apps/modules/12.0/auth_api_key/
api_key = 123456789
localhost:8069/my_service?api_key=123456789

wher is my configuration went wrong ?

@http.route('/my_service', auth='api_key', type='json')
def my_service(self, *args, **kwargs):
pass    
1
Avatar
Opusti
DevHerles
Hey @Usman, how did you do it?
Usman Farzand

you can see the answer

OdooBot
Hello,
Thank you for your email. I am currently out of the office due to illness. I am  unable to respond to messages at this time.
Thank you for your understanding. I will respond to your message as soon as I am able.
Avatar
Michael Jurke
Best Answer

If you do not like to use the addon, you can add your own auth method and use the Developer API Key of Odoo 14 (you can add API keys in the preferences now):



class IrHttp(models.AbstractModel):
_inherit = "ir.http"

@classmethod
def _auth_method_my_api_key(cls):
api_key = request.httprequest.headers.get("Authorization")
if not api_key:
raise BadRequest("Authorization header with API key missing")

user_id = request.env["res.users.apikeys"]._check_credentials(
scope="rpc", key=api_key
)
if not user_id:
raise BadRequest("API key invalid")

request.uid = user_id


And then use it

@http.route(..., auth='my_api_key', ...)


For requests, you just need to set the "Authorization: <api_key>" Header.


(Feedback appreciated)


3
Avatar
Opusti
Luke

Where should this class go? I've tried putting in my controller file but it's causing a 500 server error.

Michael Jurke

@Luke the "ir.http" is a model to be put in the /models directory. You just extend the default Odoo "ir.http" model with your API authentication.

Nicky

Thank you Michael Jurke,
your Explanation is the correct one 👍
In Odoo V14/15 are following auth options existing by default without additional apps:
thing, calendar, outlook, public, user, none

Benjamin Damour

Thank you for your reply, it helps a lot.
FYI on V16 we should use request.update_env(user_id) instead of the request.uid = user_id

Avatar
Usman Farzand
Avtor Best Answer

yes thanks i found solution to pass api key from header its working now




@http.route('/my_service', auth='api_key', type='http')
def api_get_video(self, code):
2
Avatar
Opusti
Avatar
Eric
Best Answer

This implementation below enables you to use a decorator without further inheritances.

from odoo import SUPERUSER_ID

from odoo.exceptions import ValidationError as VE

from odoo.http import request, db_list


  1. Decorator

def authenticate(func):

    @wraps(func)

    def validate_api_key(*args, **kw):

        auth_error = messages.get('access')

        key = request.httprequest.headers.get('api-key')

        if not key:

            raise VE(auth_error)

        if not request.session.db:

            request.session.db = db_list()[0]  # here we pick the first available db if the instance is a multi db and db_name has not been set in the odoo.conf

        uid = request.env['res.users.apikeys'].with_user(SUPERUSER_ID)._check_credentials(scope='rpc', key=key)

        if not uid:

            logger.error(f"{func.__name__} auth_error['message']")

            raise VE(auth_error)

        request.update_env(user=uid)  # here we switch the user to the one owning this key

        return func(*args, **kw)

    return validate_api_key


2. Controller

    @authenticate

    @route(['/products'], auth="public", type='json', methods=['POST'], csrf=False)

    def create_product(self, **kw):


0
Avatar
Opusti
Avatar
Jesús María Jurado Núñez
Best Answer

@Luke, try to change @classmethod by @property, for me it works. Thanks @Michael for sharing.


Sorry again @Luke, but @Michael 's code is right but you have to consider putting it as a model, I was confused, maybe like you, and I wrote it into the controller but that's wrong.

0
Avatar
Opusti
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Prijavi
Related Posts Odgovori Prikazi Aktivnost
Error when trying to authenticate through API (v14) Solved
authentication api
Avatar
Avatar
Avatar
Avatar
Avatar
5
dec. 20
11661
Question about how to authentication using api key Solved
authentication api api-key
Avatar
Avatar
1
sep. 25
4931
API authentication not working with API key
authentication api api-key
Avatar
0
jan. 24
4072
How to authenticate users from res.users model in laradoo package
authentication api odoo
Avatar
0
jul. 21
4332
Has anyone integrated Helpdesk with Zoom for meeting scheduling?
api
Avatar
Avatar
1
avg. 25
1292
Community
  • Tutorials
  • Dokumentacija
  • Forum
Open Source
  • Prenesi
  • Github
  • Runbot
  • Prevodi
Services
  • Odoo.sh Hosting
  • Podpora
  • Nadgradnja
  • Custom Developments
  • Izobraževanje
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Sredstva blagovne znamke
  • Kontakt
  • Zaposlitve
  • Dogodki
  • Podcast
  • Blog
  • Stranke
  • Pravno • Zasebnost
  • Varnost
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now