python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

Bug when trying to authenticate

Open AntonioJCosta opened this issue 3 years ago • 4 comments

Hello. First of all, thank you for this excellent python package solution.

Description of the problem:

I am trying to authenticate using this code

from O365 import Account, FileSystemTokenBackend


def get_account_access(
    app_id: str,
    app_secret: str,
    token_path: str,
    token_filename: str,
    tenant_id=None,
):

    credentials = [app_id, app_secret]
    if tenant_id is not None:
        credentials.append(tenant_id)

    token_backend = FileSystemTokenBackend(token_path, token_filename)
    account = Account(tuple(credentials), token_backend=token_backend)
    if not account.is_authenticated:
        try:
            account.authenticate(scopes=["basic", "message_all"])
            return account
        except Exception as e:
            raise Exception(f"Authentication failed: {e}")
    return account
But when I run this I get this error: 

Traceback (most recent call last):
  File "main.py", line 25, in <module>
    account = get_account_access(APP_ID, APP_SECRET, TOKEN_PATH, TOKEN_FILENAME)
  File "/root/tools/AutomacaoAbuse/get_account_access.py", line 21, in get_account_access
    if not account.is_authenticated:
  File "/usr/local/lib/python3.6/site-packages/O365/account.py", line 69, in is_authenticated
    token = self.con.token_backend.get_token()
  File "/usr/local/lib/python3.6/site-packages/O365/utils/token.py", line 94, in get_token
    self.token = self.load_token()  # store the token in the 'token' property
  File "/usr/local/lib/python3.6/site-packages/O365/utils/token.py", line 187, in load_token
    token = self.token_constructor(self.serializer.load(token_file))
  File "/usr/lib64/python3.6/json/__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python3.6/json/decoder.py", line 342, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 13 column 2 (char 1922)

I am running this code on my personal machine and it's working fine, with the same python version and package version.

Package version that I am using: 2.0.19 Python version: 3.6.8

May you help me with this issue? Thank you for your attention.

AntonioJCosta avatar Jul 08 '22 00:07 AntonioJCosta

It seems that the token file stored is malformed. Try to delete it, authenticate again and don't touch the file manually. It should work without issues.

alejcas avatar Jul 11 '22 09:07 alejcas

Even when the backend token is empty I am getting this error.

AntonioJCosta avatar Jul 11 '22 19:07 AntonioJCosta

Token backend is always FileSystemTokenBackend by default

alejcas avatar Jul 12 '22 18:07 alejcas

I was also getting this issue, but have resolved by deleting the token.

Is there anyway to ensure this does not happen again? Thank you

Lakehurst-RHUL avatar Apr 17 '24 15:04 Lakehurst-RHUL