Skip to content

Authenticate Public API request

Bishal Pun edited this page Jun 3, 2018 · 2 revisions

Authenticate public API request

This section shows the steps to authenticate the public API request sent by clients with client id and api access secret key.

Note

This tutorial is for the backend developer who are creating REST API for a client.

  1. Import the API Validator.
from odoo.addons.auth_api_hmac.auth.validator import APIValidator
  1. Call the authenticate_api method for authenticating the API request.
# -*- coding: utf-8 -*-

import werkzeug
import json
import logging

from odoo import http
from odoo.addons import oauth_provider
from odoo.addons.web.controllers.main import ensure_db
from odoo.addons.auth_api_hmac.auth.validator import APIValidator

_logger = logging.getLogger(__name__)


class OAuth2ProviderController(
        http.Controller):
    @http.route(
            '/oauth2/get_tags', type='http', auth='none', methods=['GET'])
    def test(self, *args, **kwargs):
        """ Returns the public key of the requested client """
        ensure_db()

        client = APIValidator.authenticate_api(http.request.httprequest)
        if not client:
            return self._json_response(
                data={'error': 'invalid_or_expired_token'}, status=401)

        data = {"client_id": client.identifier}
        return self._json_response(data=data)

APIOdooValidator.authenticate_api(http.request.httprequest, *args, **kwargs)

Clone this wiki locally