-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathquickstart.ts
60 lines (51 loc) · 1.53 KB
/
quickstart.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2020 Cognite AS
import { CogniteClient } from '@cognite/sdk';
import { ConfidentialClientApplication } from '@azure/msal-node';
const project: string = process.env.COGNITE_PROJECT!;
const clientId: string = process.env.CLIENT_ID!;
const clientSecret: string = process.env.CLIENT_SECRET!;
const azureTenant = process.env.AZURE_TENANT_ID!;
if (!project || !clientId || !clientSecret || !azureTenant) {
throw Error(
'You must set the environment variable AZURE_TENANT_ID, COGNITE_PROJECT, CLIENT_ID and CLIENT_SECRET'
);
}
async function quickstart() {
const pca = new ConfidentialClientApplication({
auth: {
clientId,
clientSecret,
authority: `https://login.microsoftonline.com/${azureTenant}`,
},
});
const client = new CogniteClient({
appId: 'Cognite SDK samples',
project,
baseUrl: 'https://api.cognitedata.com',
oidcTokenProvider: () =>
pca
.acquireTokenByClientCredential({
scopes: ['https://api.cognitedata.com/.default'],
skipCache: true,
})
.then((response) => response?.accessToken! as string),
});
await client.authenticate();
const info = (await client.get('/api/v1/token/inspect')).data;
console.log('tokenInfo', JSON.stringify(info, null, 2));
try {
const assets = await client.assets.list();
console.log(assets);
} catch (e) {
console.log('asset error');
console.log(e);
}
}
quickstart()
.then(() => {
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
});