/
client.js
42 lines (36 loc) · 926 Bytes
/
client.js
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
require('dotenv').config()
const axios = require('axios')
const { ISSUER, CLIENT_ID, CLIENT_SECRET, SCOPE } = process.env
const [,, url, method, body] = process.argv
if (!url) {
console.log('Usage: node client {url} [{method}] [{jsonData}]')
process.exit(1)
}
const sendAPIRequest = async () => {
try {
const auth = await axios({
url: `${ISSUER}/v1/token`,
method: 'post',
auth: {
username: CLIENT_ID,
password: CLIENT_SECRET
},
params: {
grant_type: 'client_credentials',
scope: SCOPE
}
})
const response = await axios({
url,
method: method ?? 'get',
data: (body) ? JSON.parse(body) : null,
headers: {
authorization: `${auth.data.token_type} ${auth.data.access_token}`
}
})
console.log(response.data)
} catch (error) {
console.log(`Error: ${error.message}`)
}
}
sendAPIRequest()