1818const fs = require ( 'fs' ) ;
1919const readline = require ( 'readline' ) ;
2020const { google} = require ( 'googleapis' ) ;
21- const GoogleAuth = require ( 'google-auth-library' ) ;
2221
23- // If modifying these scopes, delete your previously saved credentials
24- // at ~/.credentials/admin-reports_v1-nodejs-quickstart.json
22+ // If modifying these scopes, delete token.json.
2523const SCOPES = [ 'https://www.googleapis.com/auth/admin.reports.audit.readonly' ] ;
26- const TOKEN_DIR = ( process . env . HOME || process . env . HOMEPATH ||
27- process . env . USERPROFILE ) + '/.credentials/' ;
28- const TOKEN_PATH = TOKEN_DIR + 'admin-reports_v1-nodejs-quickstart.json' ;
24+ const TOKEN_PATH = 'token.json' ;
2925
3026// Load client secrets from a local file.
31- fs . readFile ( 'client_secret.json' , function processClientSecrets ( err , content ) {
32- if ( err ) {
33- console . log ( 'Error loading client secret file: ' + err ) ;
34- return ;
35- }
27+ fs . readFile ( 'credentials.json' , ( err , content ) => {
28+ if ( err ) return console . error ( 'Error loading client secret file' , err ) ;
29+
3630 // Authorize a client with the loaded credentials, then call the
3731 // Reports API.
3832 authorize ( JSON . parse ( content ) , listLoginEvents ) ;
@@ -46,20 +40,14 @@ fs.readFile('client_secret.json', function processClientSecrets(err, content) {
4640 * @param {function } callback The callback to call with the authorized client.
4741 */
4842function authorize ( credentials , callback ) {
49- const clientSecret = credentials . installed . client_secret ;
50- const clientId = credentials . installed . client_id ;
51- const redirectUrl = credentials . installed . redirect_uris [ 0 ] ;
52- const auth = new GoogleAuth ( ) ;
53- let oauth2Client = new auth . OAuth2 ( clientId , clientSecret , redirectUrl ) ;
43+ const { client_secret, client_id, redirect_uris} = credentials . installed ;
44+ const oauth2Client = new google . auth . OAuth2 ( client_id , client_secret , redirect_uris [ 0 ] ) ;
5445
5546 // Check if we have previously stored a token.
56- fs . readFile ( TOKEN_PATH , function ( err , token ) {
57- if ( err ) {
58- getNewToken ( oauth2Client , callback ) ;
59- } else {
60- oauth2Client . credentials = JSON . parse ( token ) ;
61- callback ( oauth2Client ) ;
62- }
47+ fs . readFile ( TOKEN_PATH , ( err , token ) => {
48+ if ( err ) return getNewToken ( oauth2Client , callback ) ;
49+ oauth2Client . credentials = JSON . parse ( token ) ;
50+ callback ( oauth2Client ) ;
6351 } ) ;
6452}
6553
@@ -76,18 +64,16 @@ function getNewToken(oauth2Client, callback) {
7664 access_type : 'offline' ,
7765 scope : SCOPES ,
7866 } ) ;
79- console . log ( 'Authorize this app by visiting this url: ' , authUrl ) ;
67+ console . log ( 'Authorize this app by visiting this url:' , authUrl ) ;
8068 const rl = readline . createInterface ( {
8169 input : process . stdin ,
8270 output : process . stdout ,
8371 } ) ;
84- rl . question ( 'Enter the code from that page here: ' , function ( code ) {
72+ rl . question ( 'Enter the code from that page here: ' , ( code ) => {
8573 rl . close ( ) ;
86- oauth2Client . getToken ( code , function ( err , token ) {
87- if ( err ) {
88- console . log ( 'Error while trying to retrieve access token' , err ) ;
89- return ;
90- }
74+ oauth2Client . getToken ( code , ( err , token ) => {
75+ if ( err ) return console . error ( 'Error while trying to retrieve access token' , err ) ;
76+
9177 oauth2Client . credentials = token ;
9278 storeToken ( token ) ;
9379 callback ( oauth2Client ) ;
@@ -101,15 +87,10 @@ function getNewToken(oauth2Client, callback) {
10187 * @param {Object } token The token to store to disk.
10288 */
10389function storeToken ( token ) {
104- try {
105- fs . mkdirSync ( TOKEN_DIR ) ;
106- } catch ( err ) {
107- if ( err . code != 'EEXIST' ) {
108- throw err ;
109- }
110- }
111- fs . writeFile ( TOKEN_PATH , JSON . stringify ( token ) ) ;
112- console . log ( 'Token stored to ' + TOKEN_PATH ) ;
90+ fs . writeFile ( TOKEN_PATH , JSON . stringify ( token ) , ( err ) => {
91+ if ( err ) return console . warn ( `Token not stored to ${ TOKEN_PATH } ` , err ) ;
92+ console . log ( `Token stored to ${ TOKEN_PATH } ` ) ;
93+ } ) ;
11394}
11495
11596/**
@@ -118,28 +99,23 @@ function storeToken(token) {
11899 * @param {google.auth.OAuth2 } auth An authorized OAuth2 client.
119100 */
120101function listLoginEvents ( auth ) {
121- const service = google . admin ( 'reports_v1' ) ;
102+ const service = google . admin ( { version : 'reports_v1' , auth } ) ;
122103 service . activities . list ( {
123- auth : auth ,
124104 userKey : 'all' ,
125105 applicationName : 'login' ,
126106 maxResults : 10 ,
127- } , function ( err , response ) {
128- if ( err ) {
129- console . log ( 'The API returned an error: ' + err ) ;
130- return ;
131- }
132- const activities = response . items ;
133- if ( activities . length == 0 ) {
134- console . log ( 'No logins found.' ) ;
135- } else {
107+ } , ( err , res ) => {
108+ if ( err ) return console . error ( 'The API returned an error:' , err . message ) ;
109+
110+ const activities = res . data . items ;
111+ if ( activities . length ) {
136112 console . log ( 'Logins:' ) ;
137- for ( let i = 0 ; i < activities . length ; i ++ ) {
138- const activity = activities [ i ] ;
139- console . log ( '%s: %s (%s)' , activity . id . time , activity . actor . email ,
140- activity . events [ 0 ] . name ) ;
141- }
113+ activities . forEach ( ( activity ) => {
114+ console . log ( ` ${ activity . id . time } : ${ activity . actor . email } ( ${ activity . events [ 0 ] . name } )` ) ;
115+ } ) ;
116+ } else {
117+ console . log ( 'No logins found.' ) ;
142118 }
143- } ) ;
119+ } ) ;
144120}
145121// [END admin_sdk_reports_quickstart]
0 commit comments