@@ -20,69 +20,70 @@ async function getFromApi(accessToken, resourceUrl, queryParams = {}) {
20
20
baseURL : baseApiUri ,
21
21
headers : {
22
22
"Authorization" : `Bearer ${ accessToken } ` ,
23
- "Accept" : "application/json" ,
23
+ "Accept" : "application/json" ,
24
24
"Content-Type" : "application/json; charset=utf-8"
25
25
}
26
26
} ) ;
27
-
27
+
28
28
await api . get ( resourceUrl , { params : queryParams } )
29
29
. then ( function ( apiResponse ) {
30
30
if ( apiResponse . status != '200' ) {
31
31
logResponse ( apiResponse ) ;
32
32
}
33
33
else {
34
34
data = apiResponse . data ;
35
- } } )
35
+ }
36
+ } )
36
37
. catch ( function ( error ) {
37
38
logResponse ( error ) ;
38
39
} ) ;
39
40
return data ;
40
41
}
41
42
42
43
async function getAccessToken ( ) {
43
- const audienceString = `${ baseApiUri } /identityserver/connect/token` ;
44
-
45
- const assertion = {
46
- "iss" : rksServiceAccount ,
47
- "sub" : rksServiceAccount ,
48
- "aud" : audienceString ,
49
- "exp" : Math . floor ( new Date ( ) . getTime ( ) / 1000 ) + 200 ,
50
- "jti" : uuidv4 ( )
51
- } ;
44
+ const audienceString = `${ baseApiUri } /identityserver/connect/token` ;
52
45
53
- var signedAssertion ;
54
- try {
55
- signedAssertion = jwt . sign ( assertion , privateKey , { algorithm : 'RS256' } ) ;
56
- }
57
- catch ( err ) {
58
- console . log ( `Error signing JWT. Check your private key. Error: ${ err } ` ) ;
59
- return null ;
60
- }
61
-
62
- const payload = {
63
- scope : "api" ,
64
- grant_type : "client_credentials" ,
65
- client_assertion_type : "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" ,
66
- client_assertion : signedAssertion
67
- } ;
46
+ const assertion = {
47
+ "iss" : rksServiceAccount ,
48
+ "sub" : rksServiceAccount ,
49
+ "aud" : audienceString ,
50
+ "exp" : Math . floor ( new Date ( ) . getTime ( ) / 1000 ) + 200 ,
51
+ "jti" : uuidv4 ( )
52
+ } ;
68
53
69
- const tokenResponse = await makeAccessTokenRequest ( payload ) ;
70
- if ( ! tokenResponse || ! tokenResponse . access_token ) {
71
- return null ;
72
- }
73
- return tokenResponse . access_token ;
54
+ var signedAssertion ;
55
+ try {
56
+ signedAssertion = jwt . sign ( assertion , privateKey , { algorithm : 'RS256' } ) ;
57
+ }
58
+ catch ( err ) {
59
+ console . log ( `Error signing JWT. Check your private key. Error: ${ err } ` ) ;
60
+ return null ;
61
+ }
62
+
63
+ const payload = {
64
+ scope : "api" ,
65
+ grant_type : "client_credentials" ,
66
+ client_assertion_type : "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" ,
67
+ client_assertion : signedAssertion
68
+ } ;
69
+
70
+ const tokenResponse = await makeAccessTokenRequest ( payload ) ;
71
+ if ( ! tokenResponse || ! tokenResponse . access_token ) {
72
+ return null ;
73
+ }
74
+ return tokenResponse . access_token ;
74
75
}
75
76
76
77
77
78
async function makeAccessTokenRequest ( payload ) {
78
79
return axios . post ( `${ baseApiUri } /identityserver/connect/token` , querystring . stringify ( payload ) )
79
80
. then ( function ( response ) {
80
- return response . data ;
81
- } )
82
- . catch ( function ( error ) {
83
- console . log ( error ) ;
84
- return null ;
85
- } ) ;
81
+ return response . data ;
82
+ } )
83
+ . catch ( function ( error ) {
84
+ console . log ( error ) ;
85
+ return null ;
86
+ } ) ;
86
87
}
87
88
88
89
function logResponse ( response ) {
@@ -96,17 +97,17 @@ getAccessToken().then( token => {
96
97
97
98
const participantResourceUrl = `/api/v1/administration/projects/` + rksProjectId + '/participants' ;
98
99
getFromApi ( token , participantResourceUrl )
99
- . then ( data => {
100
- if ( data ) {
101
- // Uncomment this to print out the full response to the console.
102
- // logResponse(data);
103
- console . log ( `\nTotal Participants: ${ data . totalParticipants } ` ) ;
104
- } else {
105
- console . log ( "Error when accessing the API." ) ;
106
- }
107
- } ) ;
108
- }
109
- else {
110
- console . log ( "Error obtaining access token." ) ;
111
- }
112
- } ) ;
100
+ . then ( data => {
101
+ if ( data ) {
102
+ // Uncomment this to print out the full response to the console.
103
+ // logResponse(data);
104
+ console . log ( `\nTotal Participants: ${ data . totalParticipants } ` ) ;
105
+ } else {
106
+ console . log ( "Error when accessing the API." ) ;
107
+ }
108
+ } ) ;
109
+ }
110
+ else {
111
+ console . log ( "Error obtaining access token." ) ;
112
+ }
113
+ } ) ;
0 commit comments