Solved

Sudden error in OAuth authentication

  • 26 July 2023
  • 3 replies
  • 79 views

I have a helper function that returns a CogniteClient via interactive OAuth authentication. This worked literally yesterday, but gives me an error today.

This is my function definition

 

import os
from cognite.client import CogniteClient, ClientConfig
from cognite.client.credentials import OAuthInteractive

from typing import Union


def authenticate_to_cognite_oauth(
project: str="akerbp",
client_name: str="Employee",
client_id: Union[str, type(None)]=None,
tenant_id: Union[str, type(None)]=None,
base_url: str="https://api.cognitedata.com"
) -> CogniteClient:
"""Authenticate to Cognite via OAuth interactive login.
Return cognite.client.CogniteClient."""
if client_id is None: client_id = os.getenv("CDF_CLIENT_ID")
if tenant_id is None: tenant_id = os.getenv("CDF_TENANT_ID")

authority_url = f"https://login.microsoftonline.com/{tenant_id}"
scopes = [f"{base_url}/.default"]

creds = OAuthInteractive(
authority_url=authority_url,
client_id=client_id,
scopes=scopes
)
config = ClientConfig(
client_name=client_name,
project=project,
credentials=creds,
base_url=base_url
)
return CogniteClient(config)

 

I use this function in the following way (auth function stored in auth.py)

from auth import authenticate_to_cognite_oauth
from deploy import external_id

client = authenticate_to_cognite_oauth(
project="akerbp-sandbox"
)

call = client.functions.call(external_id=external_id)
print(call.get_response())

 

The error traceback is given below. It is not able to verify the credentials, as it is NoneType instread of type Dict[str, Any]. What might cause this all of a sudden?

 

Traceback (most recent call last):
  File "/home/brakjen/dev/akerbp/demo_cognite_function/call_function.py", line 8, in <module>
    call = client.functions.call(external_id=external_id)
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/_api/functions.py", line 386, in call
    id = _get_function_internal_id(self._cognite_client, identifier)
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/_api/functions.py", line 57, in _get_function_internal_id
    function = cognite_client.functions.retrieve(external_id=primitive)
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/_api/functions.py", line 316, in retrieve
    return self._retrieve_multiple(identifiers=identifiers, resource_cls=Function, list_cls=FunctionList)
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/_api_client.py", line 343, in _retrieve_multiple
    utils._concurrency.collect_exc_info_and_raise(tasks_summary.exceptions)
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/utils/_concurrency.py", line 121, in collect_exc_info_and_raise
    raise unknown_exc
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/utils/_concurrency.py", line 270, in execute_tasks
    res = f.result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/usr/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/_api_client.py", line 143, in _post
    return self._do_request(
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/_api_client.py", line 156, in _do_request
    headers = self._configure_headers(accept, additional_headers=self._config.headers.copy())
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/_api_client.py", line 198, in _configure_headers
    self._refresh_auth_header(headers)
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/_api_client.py", line 212, in _refresh_auth_header
    auth_header_name, auth_header_value = self._config.credentials.authorization_header()
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/credentials.py", line 112, in authorization_header
    self.__access_token, self.__access_token_expires_at = self._refresh_access_token()
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/credentials.py", line 297, in _refresh_access_token
    self._verify_credentials(credentials)
  File "/home/brakjen/dev/akerbp/demo_cognite_function/.venv/lib/python3.10/site-packages/cognite/client/credentials.py", line 97, in _verify_credentials
    if "access_token" in credentials and "expires_in" in credentials:
TypeError: argument of type 'NoneType' is not iterable

 

icon

Best answer by Carin Meems 9 August 2023, 07:12

View original

3 replies

Userlevel 4
Badge +2

Hi @Anders Brakestad,

I will create a Support ticket, so our team can follow up and help you.

Best regards,

Dilini 

Userlevel 3

For anyone following, the solution to this issue (which @Anders Brakestad found himself) is to delete the old cached token. This can be achieved by first printing out the path the where it is stored:

print(creds._token_cache_path)

 

and then removing this from the terminal. After this the authentication opened the browser window and the code executed normally.

 

It might be a good idea to store the cache in the project directory. Cache location can be set in arguments to the OAuthInteractice class in the cognite sdk. It will perhaps be easier to remeber to delete it when the auth fails if it stares you in the eye. 

Reply