Skip to content

Commit d9c4dcb

Browse files
committed
Merge branch 'master' of github.com:gsuitedevs/node-samples
2 parents a92fcff + d3e04bf commit d9c4dcb

15 files changed

Lines changed: 143 additions & 219 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ node_modules/
2121
client_secret.json
2222
application_credentials.json
2323
credentials.json
24+
token.json

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Node.js samples for [G Suite API](https://developers.google.com/gsuite/) docs.
44

55
| | **Apps&nbsp;Script** <img src="https://www.gstatic.com/images/branding/product/2x/apps_script_96dp.png" align="center" width="76px"/> | **Calendar** <img src="https://www.gstatic.com/images/branding/product/2x/calendar_96dp.png" align="left" width="96px"/> | **Classroom** <img src="https://www.gstatic.com/images/branding/product/2x/classroom_96dp.png" align="left" width="96px"/> | **Drive V3** <img src="https://www.gstatic.com/images/branding/product/2x/drive_96dp.png" align="left" width="96px"/> | **Gmail** <img src="https://www.gstatic.com/images/branding/product/2x/gmail_96dp.png" align="left" width="96px"/> | **Sheets** <img src="https://www.gstatic.com/images/branding/product/2x/sheets_96dp.png" align="left" width="96px"/> | **Slides** <img src="https://www.gstatic.com/images/branding/product/2x/slides_96dp.png" align="left" width="96px"/> | **Tasks** <img src="https://www.gstatic.com/images/branding/product/2x/tasks_96dp.png" align="left" width="96px"/> |
66
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
7-
| Quickstart | [Link](https://developers.google.com/apps-script/api/quickstart/nodejs) | [Link](https://developers.google.com/google-apps/calendar/quickstart/nodejs) | [Link](https://developers.google.com/classroom/quickstart/nodejs) | [Link](https://developers.google.com/drive/v3/web/quickstart/nodejs) | [Link](https://developers.google.com/gmail/api/quickstart/nodejs) | [Link](https://developers.google.com/sheets/api/quickstart/nodejs) | [Link](https://developers.google.com/slides/quickstart/nodejs) | [Link](https://developers.google.com/google-apps/tasks/quickstart/nodejs) |
7+
| Quickstart | [Link](https://developers.google.com/apps-script/api/quickstart/nodejs) | [Link](https://developers.google.com/google-apps/calendar/quickstart/nodejs) | [Link](https://developers.google.com/classroom/quickstart/nodejs) | [Link](https://developers.google.com/drive/v3/web/quickstart/nodejs) | [Link](https://developers.google.com/gmail/api/quickstart/nodejs) | [Link](https://developers.google.com/sheets/api/quickstart/nodejs) | [Link](https://developers.google.com/slides/quickstart/nodejs) | [Link](https://developers.google.com/tasks/quickstart/nodejs) |
88
| Snippets | --- | [Link](https://developers.google.com/calendar/overview) | [Link](https://developers.google.com/classroom/guides/get-started) | [Link](https://developers.google.com/drive/v3/web/about-sdk) | [Link](https://developers.google.com/gmail/api/guides/) | [Link](https://developers.google.com/sheets/api/guides/concepts) | [Link](https://developers.google.com/slides/how-tos/overview) | --- |
99

1010
## Sample Types
@@ -27,7 +27,7 @@ Learn how to use functions within an API, such as creating a new Google Slide to
2727

2828
## Setup
2929

30-
1. Install [Node.js v4.5.0 or greater](https://nodejs.org).
30+
1. Install [Node.js v6.4.0 or greater](https://nodejs.org).
3131
1. Clone this repository.
3232
1. Follow the folder README instructions to run and test samples.
3333

adminSDK/directory/index.js

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@
1818
const fs = require('fs');
1919
const readline = require('readline');
2020
const {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-directory_v1-nodejs-quickstart.json
22+
// If modifying these scopes, delete token.json.
2523
const SCOPES = ['https://www.googleapis.com/auth/admin.directory.user'];
26-
const TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
27-
process.env.USERPROFILE) + '/.credentials/';
28-
const TOKEN_PATH = TOKEN_DIR + 'admin-directory_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
// Directory API.
3832
authorize(JSON.parse(content), listUsers);
@@ -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
*/
4842
function 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
*/
10389
function 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,26 +99,22 @@ function storeToken(token) {
11899
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
119100
*/
120101
function listUsers(auth) {
121-
const service = google.admin('directory_v1');
102+
const service = google.admin({version: 'directory_v1', auth});
122103
service.users.list({
123-
auth: auth,
124104
customer: 'my_customer',
125105
maxResults: 10,
126106
orderBy: 'email',
127-
}, function(err, response) {
128-
if (err) {
129-
console.log('The API returned an error: ' + err);
130-
return;
131-
}
132-
const users = response.users;
133-
if (users.length == 0) {
134-
console.log('No users in the domain.');
135-
} else {
107+
}, (err, res) => {
108+
if (err) return console.error('The API returned an error:', err.message);
109+
110+
const users = res.data.users;
111+
if (users.length) {
136112
console.log('Users:');
137-
for (let i = 0; i < users.length; i++) {
138-
const user = users[i];
139-
console.log('%s (%s)', user.primaryEmail, user.name.fullName);
140-
}
113+
users.forEach((user) => {
114+
console.log(`${user.primaryEmail} (${user.name.fullName})`);
115+
});
116+
} else {
117+
console.log('No users found.');
141118
}
142119
});
143120
}

adminSDK/reports/index.js

Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@
1818
const fs = require('fs');
1919
const readline = require('readline');
2020
const {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.
2523
const 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
*/
4842
function 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
*/
10389
function 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
*/
120101
function 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

Comments
 (0)