Solved

Python SDK Azure B2C Authentication

  • 9 December 2021
  • 1 reply
  • 58 views

Userlevel 4
Badge

Hi there,

I am trying to authenticate the CogniteClient in bluefield using our Azure B2C:

 

response = requests.request("POST", "https://oceandataplatform.b2clogin.com/oceandataplatform.onmicrosoft.com/B2C_1A_ROPC_Auth/oauth2/v2.0/token", headers={
'Content-Type': 'application/x-www-form-urlencoded'
}, data={
"grant_type": 'password',
"client_id": client_id,
"scope": 'openid https://westeurope-1.cognitedata.com/user_impersonation',
"username": username,
"password": password,
})

creds = response.json()

client = CogniteClient(
token=creds["access_token"],
project="oceandata",
base_url='https://westeurope-1.cognitedata.com',
client_name="cognite-python-dev",
debug=True,
)

This works perfectly :)

 

Edit: I initially asked how to get this working before spotting a typo in the code. Everything works fine now. Leaving the post here so that others may use it for reference.

icon

Best answer by thomafred 9 December 2021, 13:52

View original

1 reply

Userlevel 4
Badge

FYI - The `token`-argument in `CogniteClient` can also be callable:

 

def get_token():
print("----> Fetching bearer token")

response = requests.request("POST", url, headers={
'Content-Type': 'application/x-www-form-urlencoded'
}, data={
"grant_type": 'password',
"client_id": client_id,
"scope": 'openid https://westeurope-1.cognitedata.com/user_impersonation',
"username": username,
"password": password,
})
creds = response.json()

return creds["access_token"]

client = CogniteClient(
token=get_token,
project="oceandata",
base_url='https://westeurope-1.cognitedata.com',
client_name="cognite-python-dev",
debug=True,
)

print(client.iam.token.inspect())

 

Reply