-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
67 lines (54 loc) · 1.79 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const dotenv = require('dotenv').config();
const { google } = require('googleapis');
const key = require('./auth.json');
const fs = require('fs');
// Sets Analytics API scope: read only (to view reports)
const ANALYTICS_SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'];
//Sets Docs API scope:
const DOCS_SCOPES = ['https://www.googleapis.com/auth/drive'];
const DRIVE_SCOPES = ['https://www.googleapis.com/auth/drive'];
// Path to service account json
const SERVICE_ACCOUNT_FILE = './auth.json';
// Create a new JWT client using the key file downloaded from the Google Developer Console
const authJWT = new google.auth.GoogleAuth({
keyFile: SERVICE_ACCOUNT_FILE,
scopes: [ANALYTICS_SCOPES, DOCS_SCOPES, DRIVE_SCOPES],
});
const docsAuthJWT = new google.auth.GoogleAuth({
keyFile: SERVICE_ACCOUNT_FILE,
scopes: DOCS_SCOPES,
});
async function authorizeAnalytics() {
// Authorization
console.log('Authorization: Generating OAuth 2.0 Token...\n');
const client = await authJWT.getClient();
// Obtains new analytics client, making sure we are authorized
const analytics = google.analyticsreporting({
version: 'v4',
auth: client,
});
return analytics;
}
async function authorizeDocs() {
const client = await authJWT.getClient();
// Obtains new analytics client, making sure we are authorized
const docs = google.docs({
version: 'v1',
auth: client,
});
return docs;
}
async function authorizeDrive() {
const client = await authJWT.getClient();
// Obtains new analytics client, making sure we are authorized
const drive = google.drive({
version: 'v3',
auth: client,
});
return drive;
}
module.exports = {
authorize: authorizeAnalytics,
authorizeDocs,
authorizeDrive,
};