Skip to content

Fail to login using api #771

@arianpasquali

Description

@arianpasquali

I have been trying to use the http api in order to programmatically insert documents and labels.
However I didn't find a way to make login work yet. Have been trying a few doccano api clients and a code suggestion in another issue but I get the same error.

How to reproduce the behaviour

import requests
import json

class Client(object):
    """
    Client code was inspired by: https://github.com/chakki-works/doccano/issues/6#issuecomment-489924577
    Endpoints can be found here: https://github.com/chakki-works/doccano/blob/master/app/api/urls.py
    """
    def __init__(self, entrypoint, username=None, password=None):
        self.entrypoint = entrypoint
        self.client = requests.Session()
        self._login(username, password)

    def _login(self, username, password):
        url = f"{self.entrypoint}/v1/auth-token"
        login = {"username": username, "password": password}
        response = self.client.post(url, json=login)
        api_token = response.json()["token"]
        self.client.headers.update({"Authorization": f"Token {api_token}"})


doccano_client = Client("http://localhost:3000", "admin", "password")

200 http status code as response, but also a JSONDecodeError response

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-6-512f67c518b4> in <module>
      1 import requests
----> 2 doccano_client = Client("http://localhost:3000", "admin", "password")

<ipython-input-4-525fc6bc89a1> in __init__(self, entrypoint, username, password)
      7         self.entrypoint = entrypoint
      8         self.client = requests.Session()
----> 9         self._login(username, password)
     10 
     11     def _login(self, username, password):

<ipython-input-4-525fc6bc89a1> in _login(self, username, password)
     13         login = {"username": username, "password": password}
     14         response = self.client.post(url, json=login)
---> 15         api_token = response.json()["token"]
     16         self.client.headers.update({"Authorization": f"Token {api_token}"})
     17 

~/anaconda3/lib/python3.7/site-packages/requests/models.py in json(self, **kwargs)
    895                     # used.
    896                     pass
--> 897         return complexjson.loads(self.text, **kwargs)
    898 
    899     @property

~/anaconda3/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

~/anaconda3/lib/python3.7/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~/anaconda3/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Your Environment

  • Operating System: ubuntu 20.04
  • Python Version Used: Python 3
  • When you install doccano: May 2020
  • How did you install doccano (Heroku button etc): git clone, docker-compose dev up

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions