Skip to content

Instantly share code, notes, and snippets.

@ks93
Created August 18, 2022 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ks93/bf156966291684a64e17fd4fd77780be to your computer and use it in GitHub Desktop.
Save ks93/bf156966291684a64e17fd4fd77780be to your computer and use it in GitHub Desktop.
Open Industrial Data with OIDC
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "4e532dd4",
"metadata": {},
"source": [
"To access Open Industrial Data with OIDC you need to sign up to Cognite Hub at https://hub.cognite.com/.\n",
"\n",
"The following example requires Python packages `cognite-sdk` version 4.0.0 or greater and Microsoft's `msal` for interactive login using the [device code flow](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-device-code)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cd11f3fe",
"metadata": {},
"outputs": [],
"source": [
"from cognite.client import CogniteClient, ClientConfig\n",
"from cognite.client.credentials import Token\n",
"from msal import PublicClientApplication\n",
"\n",
"TENANT_ID = \"48d5043c-cf70-4c49-881c-c638f5796997\"\n",
"CLIENT_ID = \"1b90ede3-271e-401b-81a0-a4d52bea3273\"\n",
"CDF_CLUSTER = \"api\"\n",
"\n",
"SCOPES = [f\"https://{CDF_CLUSTER}.cognitedata.com/.default\"]\n",
"\n",
"AUTHORITY_HOST_URI = \"https://login.microsoftonline.com\"\n",
"AUTHORITY_URI = AUTHORITY_HOST_URI + \"/\" + TENANT_ID"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e9af1074",
"metadata": {},
"outputs": [],
"source": [
"app = PublicClientApplication(client_id=CLIENT_ID, authority=AUTHORITY_URI)\n",
"\n",
"def authenticate_device_code(app):\n",
" # Firstly, check the cache to see if this end user has signed in before\n",
" accounts = app.get_accounts()\n",
" if accounts:\n",
" creds = app.acquire_token_silent(SCOPES, account=accounts[0])\n",
" else:\n",
" device_flow = app.initiate_device_flow(scopes=SCOPES)\n",
" print(device_flow[\"message\"]) # print device code to screen\n",
" creds = app.acquire_token_by_device_flow(flow=device_flow)\n",
" return creds\n",
"\n",
"\n",
"def get_token():\n",
" return authenticate_device_code(app)[\"access_token\"]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "93a9a639",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code DMQ563CFQ to authenticate.\n"
]
},
{
"data": {
"text/plain": [
"[{\n",
" \"url_name\": \"publicdata\",\n",
" \"groups\": [\n",
" 4860375157547584,\n",
" 5824342682088767\n",
" ]\n",
" }]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"config = ClientConfig(\n",
" client_name=\"so-industrial-client\",\n",
" project=\"publicdata\",\n",
" credentials=Token(get_token)\n",
")\n",
"client = CogniteClient(config)\n",
"client.iam.token.inspect().projects # To verify that authentication was successful"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a77347c9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>external_id</th>\n",
" <th>name</th>\n",
" <th>parent_id</th>\n",
" <th>parent_external_id</th>\n",
" <th>description</th>\n",
" <th>metadata</th>\n",
" <th>id</th>\n",
" <th>created_time</th>\n",
" <th>last_updated_time</th>\n",
" <th>root_id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>WMT:23-TE-96116-04</td>\n",
" <td>23-TE-96116-04</td>\n",
" <td>3117826349444493</td>\n",
" <td>WMT:23-DB-9101</td>\n",
" <td>VRD - PH 1STSTGGEAR THRUST BRG OUT</td>\n",
" <td>{'ELC_STATUS_ID': '1211', 'RES_ID': '525283', ...</td>\n",
" <td>702630644612</td>\n",
" <td>0</td>\n",
" <td>1659105542151</td>\n",
" <td>6687602007296940</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>WMT:23-TE-96148</td>\n",
" <td>23-TE-96148</td>\n",
" <td>8515799768286580</td>\n",
" <td>WMT:23-TT-96148</td>\n",
" <td>VRD - PH 1STSTG COMP SEAL GAS HTR</td>\n",
" <td>{'ELC_STATUS_ID': '1211', 'RES_ID': '532924', ...</td>\n",
" <td>5156972057719</td>\n",
" <td>0</td>\n",
" <td>1659105542254</td>\n",
" <td>6687602007296940</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>WMT:23-YT-96117-01</td>\n",
" <td>23-YT-96117-01</td>\n",
" <td>3257705896277160</td>\n",
" <td>WMT:23-YE-96117-01</td>\n",
" <td>VRD - PH 1STSTGGEAR 1 JOURNBRG DE</td>\n",
" <td>{'ELC_STATUS_ID': '1211', 'RES_ID': '446683', ...</td>\n",
" <td>8019487489463</td>\n",
" <td>0</td>\n",
" <td>1659105542348</td>\n",
" <td>6687602007296940</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>WMT:23-FI-96151</td>\n",
" <td>23-FI-96151</td>\n",
" <td>4239585628663887</td>\n",
" <td>WMT:23-FT-96151</td>\n",
" <td>SOFT TAG VRD - PH 1STSTG PRIM SEAL LEAK DE</td>\n",
" <td>{'ELC_STATUS_ID': '1211', 'SOURCE_DB': 'workma...</td>\n",
" <td>9258567430091</td>\n",
" <td>0</td>\n",
" <td>1659105542441</td>\n",
" <td>6687602007296940</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>WMT:23-LT-92521</td>\n",
" <td>23-LT-92521</td>\n",
" <td>2069232457199305</td>\n",
" <td>WMT:23-LV-92521A</td>\n",
" <td>VRD - PH 1STSTGSUCTSCRUBBER LEVEL</td>\n",
" <td>{'ELC_STATUS_ID': '1211', 'RES_ID': '523206', ...</td>\n",
" <td>12670864495024</td>\n",
" <td>0</td>\n",
" <td>1659105542519</td>\n",
" <td>6687602007296940</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"AssetList([<cognite.client.data_classes.assets.Asset at 0x11346c4f0>,\n",
" <cognite.client.data_classes.assets.Asset at 0x113498280>,\n",
" <cognite.client.data_classes.assets.Asset at 0x113499150>,\n",
" <cognite.client.data_classes.assets.Asset at 0x113498af0>,\n",
" <cognite.client.data_classes.assets.Asset at 0x113498f10>])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.assets.list(limit=5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4468c4d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.3 ('.venv': poetry)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.3"
},
"vscode": {
"interpreter": {
"hash": "7d58e0c6a71764346407e330cd0f8ff4254611feb02e8d786d5496d9ef5b329c"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment