Skip to content

Commit 6a1914b

Browse files
committed
remove bin from gitignore
1 parent 73b40ea commit 6a1914b

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ x86/
2626
[Aa][Rr][Mm]/
2727
[Aa][Rr][Mm]64/
2828
bld/
29-
[Bb]in/
3029
[Oo]bj/
3130
[Ll]og/
3231
[Ll]ogs/

bin/auth.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const msal = require('@azure/msal-node');
2+
3+
const msalConfig = {
4+
auth: {
5+
clientId: process.env.CLIENT_ID,
6+
authority: process.env.AAD_ENDPOINT + process.env.TENANT_ID,
7+
clientSecret: process.env.CLIENT_SECRET,
8+
}
9+
};
10+
11+
// With client credentials flows permissions need to be granted in the portal by a tenant administrator.
12+
// The scope is always in the format '<resource>/.default'.
13+
const tokenRequest = {
14+
scopes: [process.env.GRAPH_ENDPOINT + '.default'],
15+
};
16+
17+
const apiConfig = {
18+
uri: process.env.GRAPH_ENDPOINT + 'v1.0/users',
19+
};
20+
21+
// Create msal application object
22+
const cca = new msal.ConfidentialClientApplication(msalConfig);
23+
24+
async function getToken(tokenRequest) {
25+
return await cca.acquireTokenByClientCredential(tokenRequest);
26+
}
27+
28+
module.exports = {
29+
apiConfig: apiConfig,
30+
tokenRequest: tokenRequest,
31+
getToken: getToken
32+
};

bin/fetch.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const axios = require('axios');
2+
3+
async function callApi(endpoint, accessToken) {
4+
5+
const options = {
6+
headers: {
7+
Authorization: `Bearer ${accessToken}`
8+
}
9+
};
10+
11+
console.log('request made to web API at: ' + new Date().toString());
12+
13+
try {
14+
const response = await axios.default.get(endpoint, options);
15+
return response.data;
16+
} catch (error) {
17+
console.log(error)
18+
return error;
19+
}
20+
};
21+
22+
module.exports = {
23+
callApi: callApi
24+
};

bin/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
3+
// read in env settings
4+
require('dotenv').config();
5+
6+
const yargs = require('yargs');
7+
8+
const fetch = require('./fetch');
9+
const auth = require('./auth');
10+
11+
const options = yargs
12+
.usage('Usage: --op <operation_name>')
13+
.option('op', { alias: 'operation', describe: 'operation name', type: 'string', demandOption: true })
14+
.argv;
15+
16+
async function main() {
17+
console.log(`You have selected: ${options.op}`);
18+
19+
switch (yargs.argv['op']) {
20+
case 'getUsers':
21+
22+
try {
23+
const authResponse = await auth.getToken(auth.tokenRequest);
24+
const users = await fetch.callApi(auth.apiConfig.uri, authResponse.accessToken);
25+
console.log(users);
26+
} catch (error) {
27+
console.log(error);
28+
}
29+
30+
break;
31+
default:
32+
console.log('Select a Graph operation first');
33+
break;
34+
}
35+
};
36+
37+
main();

0 commit comments

Comments
 (0)