Solved

Error in Learn to Use the Cognite Python SDK

  • 27 February 2023
  • 8 replies
  • 162 views

Badge

Getting an error on 2_List_Search_Retrieve.ipynb:

c = cauth.create_cognite_client(method="client-secret")

AttributeError Traceback (most recent call last) c:\Users\tomas.kurten.perez\Downloads\COGNITE\using-cognite-python-sdk\notebooks\2_List_Search_Retrieve.ipynb Cell 7 in <cell line: 1>() ----> 1 c = cauth.create_cognite_client(method="client-secret") AttributeError: module 'auth' has no attribute 'create_cognite_client'

icon

Best answer by Dilini Fernando 24 March 2023, 07:08

View original

8 replies

Userlevel 3

Hi

Did you run through the 1st notebook? (1_Authentication.ipynb)
This is where the function to create the client is created and defined

Userlevel 2
Badge +6

Hi @tomaskurtenperez 

for me it works fine if I run the code as it is originally in the notebook:  

c = cauth.create_cognite_client()

 

Badge

Hi @tomaskurtenperez 

for me it works fine if I run the code as it is originally in the notebook:  

c = cauth.create_cognite_client()

 

Hi @Sofie Haug , i tried that way too, but it didn't work.
Also on the 1st notebook i got a problem with client.login.status()

CogniteAuthError: Error generating access token: invalid_client, 401, AADSTS7000216: 'client_assertion', 'client_secret' or 'request' is required for the 'client_credentials' grant type. Trace ID: 24b037b2-0788-4395-b5a6-5929b2878700 Correlation ID: 23d98b09-b4f2-44c3-8775-3e23826ae046 Timestamp: 2023-02-28 18:00:17Z

How can i resolve the problems? Can I continue with the training without using that functions? 

Userlevel 3

 Have a setup the secret in your local environment?

“Make Sure, You've created an environment variable CLIENT_SECRET with the value of the client secret obtained from admin.” 


There are multiple ways you can do that, either by using an .env file, you can get more information here 
You can also set the client secret using getpass, see more info here. The lines of code for that are in the notebook, but commented

 

The line to check if the client is connected should be

client.iam.token.inspect()

We will update the code on the notebook. 

Userlevel 2
Badge +6

@tomaskurtenperez you are depending on being able to authenticate towards CDF to be able to move on. 

@Gaetan Helness A client secret shouldn’t be necessary since in the #2 notebook, they authenticate with interactive log in (if I remember correctly)..

Userlevel 4
Badge +2

Hi @tomaskurtenperez ,

Did you manage to solve the issue and complete the course?

Best regards,
Dilini

Badge +1

Try using OAuth authentication method. 

 

from cognite.client import CogniteClient, ClientConfig

from cognite.client.credentials import OAuthClientCredentials

tenant = "xyz"
client_id = "xyz"
scope = "xyz"
cdf_cluster = "xyz"
client_secret = "xyz"
project_name="xyz"
base_url= f"https://{cdf_cluster}.cognitedata.com"
token_url= f"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token"

creds = OAuthClientCredentials(
    token_url=token_url,
    client_id=client_id,
    client_secret=client_secret,
    scopes=scope,
    )

cnf = ClientConfig(client_name="xyz", project=project_name, credentials=creds, base_url=base_url)
client = CogniteClient(cnf)

print(client.login.status())

 

fill in the environment details and this should work.

Userlevel 4
Badge +2

Hi @tomaskurtenperez,

I hope the above helped. I’m closing this thread now, please feel free to create a new post if you have any questions.

Best regards,
Dilini

 

Reply