1- import * as googleAuth from ' google-auth-library' ;
2- import { Credentials } from ' google-auth-library/build/src/auth/credentials' ;
3- import * as fs from 'fs' ;
4- import * as readline from ' readline' ;
1+ import * as googleAuth from " google-auth-library" ;
2+ import { Credentials } from " google-auth-library/build/src/auth/credentials" ;
3+ import * as fs from "fs" ;
4+ import * as readline from " readline" ;
55
6-
7- // TODO
6+ // TODO
87// * Get credentials from file in config.yaml -> google_credentials_file
98// * Generate auth URL
109// * Wait for user to come back with code
1110// * Take code and get the access token
1211// * Dump credentials data to file
1312// * Have main app read from the seprate file, rather than config.yaml
14- // * Have main app update access, refresh, and expiration configs
13+ // * Have main app update access, refresh, and expiration configs
1514// automatically
1615const scopes : Array < string > = [
17- ' https://www.googleapis.com/auth/admin.directory.group.member'
18- , ' https://www.googleapis.com/auth/admin.directory.group'
19- , ' https://mail.google.com/'
20- , ' https://www.googleapis.com/auth/gmail.modify'
21- , ' https://www.googleapis.com/auth/gmail.compose'
22- , ' https://www.googleapis.com/auth/gmail.send'
16+ " https://www.googleapis.com/auth/admin.directory.group.member" ,
17+ " https://www.googleapis.com/auth/admin.directory.group" ,
18+ " https://mail.google.com/" ,
19+ " https://www.googleapis.com/auth/gmail.modify" ,
20+ " https://www.googleapis.com/auth/gmail.compose" ,
21+ " https://www.googleapis.com/auth/gmail.send" ,
2322] ;
2423
25-
2624/**
2725 * Step 0: Create OAuth2 credentials at the Google Console (make sure to download JSON, not only just get key and secret)
2826 */
@@ -32,78 +30,68 @@ const scopes: Array<string> = [
3230 */
3331
3432export function getAuthorizeUrl (
35- credentials
36- , callback : ( err : any , url : string ) => any
33+ credentials ,
34+ callback : ( err : any , url : string ) => any
3735) : void {
3836 const oauth2Client = new googleAuth . OAuth2Client (
39- credentials . installed . client_id
40- , credentials . installed . client_secret
41- , credentials . installed . redirect_uris [ 0 ]
37+ credentials . installed . client_id ,
38+ credentials . installed . client_secret ,
39+ credentials . installed . redirect_uris [ 0 ]
4240 ) ;
4341
44- const authUrl = oauth2Client . generateAuthUrl ( {
45- access_type : ' offline' ,
46- scope : scopes
47- } ) ;
42+ const authUrl = oauth2Client . generateAuthUrl ( {
43+ access_type : " offline" ,
44+ scope : scopes ,
45+ } ) ;
4846
49- callback ( null , authUrl ) ;
47+ callback ( null , authUrl ) ;
5048}
5149
52-
5350/**
5451 * Step 2: Get auth token
5552 */
5653
57-
5854export function getAccessToken (
59- code
60- , credentials
61- , callback : ( err : any , token ?: Credentials | null ) => any ) : void
62- {
55+ code ,
56+ credentials ,
57+ callback : ( err : any , token ?: Credentials | null ) => any
58+ ) : void {
6359 const oauth2Client = new googleAuth . OAuth2Client (
64- credentials . installed . client_id
65- , credentials . installed . client_secret
66- , credentials . installed . redirect_uris [ 0 ]
60+ credentials . installed . client_id ,
61+ credentials . installed . client_secret ,
62+ credentials . installed . redirect_uris [ 0 ]
6763 ) ;
6864
69- oauth2Client . getToken ( code , ( err , token ) => {
70- if ( err ) return console . log ( err ) ;
65+ oauth2Client . getToken ( code , ( err , token ) => {
66+ if ( err ) return console . log ( err ) ;
7167
72- callback ( null , token ) ;
73- } ) ;
68+ callback ( null , token ) ;
69+ } ) ;
7470}
7571
76-
77- fs . readFile ( 'google-credentials.json' , ( err , content ) => {
78- if ( err ) return console . log ( 'Error loading client secret file:' , err ) ;
79- let credentials = JSON . parse ( content . toString ( ) ) ;
80-
81- getAuthorizeUrl (
82- credentials
83- , ( err , url ) => {
84- if ( err ) return console . log ( err ) ;
85- console . log ( "Auth url is: " , url ) ;
86-
87- let rl = readline . createInterface ( {
88- input : process . stdin
89- , output : process . stdout
72+ fs . readFile ( "google-credentials.json" , ( err , content ) => {
73+ if ( err ) return console . log ( "Error loading client secret file:" , err ) ;
74+ let credentials = JSON . parse ( content . toString ( ) ) ;
75+
76+ getAuthorizeUrl ( credentials , ( err , url ) => {
77+ if ( err ) return console . log ( err ) ;
78+ console . log ( "Auth url is: " , url ) ;
79+
80+ let rl = readline . createInterface ( {
81+ input : process . stdin ,
82+ output : process . stdout ,
83+ } ) ;
84+
85+ rl . question ( "Input token: " , ( token ) => {
86+ getAccessToken ( token , credentials , ( err , access ) => {
87+ if ( err ) return console . log ( err ) ;
88+ //console.log("Auth token is: ", token);
89+ console . log ( "google_client_token: " + token ) ;
90+ console . log ( "google_access_token: " + access [ "access_token" ] ) ;
91+ console . log ( "google_token_type: " + access [ "token_type" ] ) ;
92+ console . log ( "google_refresh_token: " + access [ "refresh_token" ] ) ;
93+ console . log ( "google_expires_date: " + access [ "expiry_date" ] ) ;
9094 } ) ;
91-
92- rl . question ( "Input token: " , ( token ) => {
93- getAccessToken ( token , credentials , ( err , access ) => {
94- if ( err ) return console . log ( err ) ;
95- //console.log("Auth token is: ", token);
96- console . log ( "google_client_token: " + token ) ;
97- console . log ( "google_access_token: "
98- + access [ 'access_token' ] ) ;
99- console . log ( "google_token_type: "
100- + access [ 'token_type' ] ) ;
101- console . log ( "google_refresh_token: "
102- + access [ 'refresh_token' ] ) ;
103- console . log ( "google_expires_date: "
104- + access [ 'expiry_date' ] ) ;
105- } ) ;
106- } ) ;
107- }
108- ) ;
95+ } ) ;
96+ } ) ;
10997} ) ;
0 commit comments