forked from yyuueexxiinngg/some-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleClient.ts
More file actions
80 lines (77 loc) · 2.62 KB
/
googleClient.ts
File metadata and controls
80 lines (77 loc) · 2.62 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
import fs from "fs";
import { OAuth2Client } from "google-auth-library";
import { drive_v3, google, oauth2_v1 } from "googleapis";
import readline from "readline";
import * as types from "./types";
class GoogleClient {
public oAuth2Client: OAuth2Client;
private options: types.GoogleClient$Options;
constructor(options: types.GoogleClient$Options) {
this.options = options;
if (!fs.existsSync(options.credentialsPath)) {
throw new Error("Credentials not exist!");
}
const credentialsFile = JSON.parse(
fs.readFileSync(options.credentialsPath).toString()
);
const credentials = credentialsFile.installed || credentialsFile.web;
this.oAuth2Client = new google.auth.OAuth2(
credentials.client_id,
credentials.client_secret,
credentials.redirect_uris[0]
);
}
public async init() {
await this.authorize();
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
*/
public async getAccessToken(oAuth2Client: OAuth2Client) {
return new Promise(resolve => {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: "offline",
scope: this.options.scopes[0]
});
console.log("Authorize this app by visiting this url:", authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter the code from that page here: ", (code: string) => {
rl.close();
oAuth2Client.getToken(code, (err: any, token: any) => {
if (err) {
return console.error("Error retrieving access token", err);
}
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(this.options.tokenPath, JSON.stringify(token), error => {
if (error) {
return console.error(error);
}
console.log("Token stored to", this.options.tokenPath);
});
this.oAuth2Client = oAuth2Client;
resolve();
});
});
});
}
private async authorize() {
return new Promise(async (resolve, reject) => {
if (fs.existsSync(this.options.tokenPath)) {
this.oAuth2Client.setCredentials(
JSON.parse(fs.readFileSync(this.options.tokenPath).toString())
);
resolve();
} else {
await this.getAccessToken(this.oAuth2Client);
resolve();
}
});
}
}
export default GoogleClient;