-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
44 lines (41 loc) · 1.46 KB
/
utils.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
export async function getOAuth2AccessToken(
clientId: string,
clientSecret: string,
identityTokenUrl: string
): Promise<string> {
if (!clientId || !clientSecret) {
console.log(
"Please provide values for clientId and clientSecret. You can find more info in the tutorial at www.dangl-it.com or the BIMCloud documenation."
);
throw new Error("Missing clientId or clientSecret");
}
try {
const tokenResponseRaw = await fetch(identityTokenUrl, {
method: "POST",
body: "grant_type=client_credentials&scope=bimcloud",
headers: {
Authorization: "Basic " + btoa(clientId + ":" + clientSecret),
"Content-Type": "application/x-www-form-urlencoded",
},
});
if (tokenResponseRaw.status !== 200) {
throw new Error(
"Failed to obtain an access token, status code: " +
tokenResponseRaw.status
);
}
const jsonResponse = await tokenResponseRaw.json();
const accessToken = jsonResponse["access_token"];
if (!accessToken) {
console.log(
"Failed to obtain an access token. Have you read the documentation and set up your OAuth2 client?"
);
}
return accessToken;
} catch {
console.log(
"Failed to obtain an access token. Have you read the documentation and set up your OAuth2 client?"
);
throw new Error("Failed to obtain an access token");
}
}