1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- /* eslint-disable camelcase */
16+
1717// [START admin_sdk_reports_quickstart]
18- import fs from 'fs/promises' ;
19- import path from 'path' ;
20- import process from 'process' ;
18+
19+ import path from 'node: path' ;
20+ import process from 'node: process' ;
2121import { authenticate } from '@google-cloud/local-auth' ;
2222import { google } from 'googleapis' ;
2323
24- // If modifying these scopes, delete token.json.
2524const SCOPES = [ 'https://www.googleapis.com/auth/admin.reports.audit.readonly' ] ;
26- // The file token.json stores the user's access and refresh tokens, and is
27- // created automatically when the authorization flow completes for the first
28- // time.
29- const TOKEN_PATH = path . join ( process . cwd ( ) , 'token.json' ) ;
3025const CREDENTIALS_PATH = path . join ( process . cwd ( ) , 'credentials.json' ) ;
3126
3227/**
33- * Reads previously authorized credentials from the save file.
34- *
35- * @return {Promise<import("google-auth-library").AuthClient|null> }
36- */
37- async function loadSavedCredentialsIfExist ( ) {
38- try {
39- const content = await fs . readFile ( TOKEN_PATH ) ;
40- const credentials = JSON . parse ( content ) ;
41- return google . auth . fromJSON ( credentials ) ;
42- } catch ( err ) {
43- return null ;
44- }
45- }
46-
47- /**
48- * Serializes credentials to a file compatible with GoogleAuth.fromJSON.
49- *
50- * @param {import("google-auth-library").AuthClient } client
51- * @return {Promise<void> }
52- */
53- async function saveCredentials ( client ) {
54- const content = await fs . readFile ( CREDENTIALS_PATH ) ;
55- const keys = JSON . parse ( content ) ;
56- const key = keys . installed || keys . web ;
57- const payload = JSON . stringify ( {
58- type : 'authorized_user' ,
59- client_id : key . client_id ,
60- client_secret : key . client_secret ,
61- refresh_token : client . credentials . refresh_token ,
62- } ) ;
63- await fs . writeFile ( TOKEN_PATH , payload ) ;
64- }
65-
66- /**
67- * Load or request or authorization to call APIs.
68- *
28+ * Lists the last 10 login events for the domain.
6929 */
70- async function authorize ( ) {
71- let client = await loadSavedCredentialsIfExist ( ) ;
72- if ( client ) {
73- return client ;
74- }
75- client = await authenticate ( {
30+ async function listLoginEvents ( ) {
31+ const auth = await authenticate ( {
7632 scopes : SCOPES ,
7733 keyfilePath : CREDENTIALS_PATH ,
7834 } ) ;
79- if ( client . credentials ) {
80- await saveCredentials ( client ) ;
81- }
82- return client ;
83- }
84-
85- /**
86- * Lists the last 10 login events for the domain.
87- *
88- * @param {import("google-auth-library").AuthClient } auth An authorized OAuth2 client.
89- */
90- async function listLoginEvents ( auth ) {
9135 const service = google . admin ( { version : 'reports_v1' , auth} ) ;
92- const res = await service . activities . list ( {
36+ const result = await service . activities . list ( {
9337 userKey : 'all' ,
9438 applicationName : 'login' ,
9539 maxResults : 10 ,
9640 } ) ;
97- const activities = res . data . items ;
41+ const activities = result . data . items ;
9842 if ( ! activities || activities . length === 0 ) {
9943 console . log ( 'No logins found.' ) ;
10044 return ;
@@ -103,10 +47,10 @@ async function listLoginEvents(auth) {
10347 console . log ( 'Logins:' ) ;
10448 activities . forEach ( ( activity ) => {
10549 console . log (
106- `${ activity . id . time } : ${ activity . actor . email } (${ activity . events [ 0 ] . name } )` ,
50+ `${ activity . id ? .time } : ${ activity . actor ? .email } (${ activity . events ?. [ 0 ] ? .name } )` ,
10751 ) ;
10852 } ) ;
10953}
11054
111- authorize ( ) . then ( listLoginEvents ) . catch ( console . error ) ;
55+ await listLoginEvents ( ) ;
11256// [END admin_sdk_reports_quickstart]
0 commit comments