Skip to main content
Answer

Python SDK Azure B2C Authentication

  • December 9, 2021
  • 1 reply
  • 68 views

thomafred
Seasoned
Forum|alt.badge.img

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.

Best answer by thomafred

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())

 

1 reply

thomafred
Seasoned
Forum|alt.badge.img
  • Author
  • Seasoned
  • Answer
  • December 9, 2021

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())