-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriveRequestHandler.js
138 lines (128 loc) · 4.73 KB
/
DriveRequestHandler.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import fetch from 'isomorphic-fetch';
export default class DriveRequestHandler{
#passwordListFileName = 'passwordList.json';
#baseUploadUrl = 'https://www.googleapis.com/upload/drive/v3/files/';
#downloadUrl = 'https://www.googleapis.com/drive/v3/files/';
#oauthUrl = 'https://oauth2.googleapis.com/token';
#apiCredentials = {
clientId: '350990865082-6s5s1l5t5l5eonkea8prah5m04unmo1n.apps.googleusercontent.com',
clientSecret: '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',
redirectUrl: 'http://localhost:8080/auth',
}
#refreshToken;
#accessToken;
async fetchRefreshToken(authorizationCode){
let parameters = {
client_id: this.#apiCredentials.clientId,
client_secret: this.#apiCredentials.clientSecret,
code: authorizationCode,
grant_type: 'authorization_code',
redirect_uri: this.#apiCredentials.redirectUrl
}
let requestOptions = {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: this.#createUrlSafeString(parameters)
}
let apiResponse = await this.requestDriveApi(this.#oauthUrl, requestOptions);
this.#refreshToken = apiResponse.refresh_token;
}
async #renewAccessToken(){
if(this.#refreshToken == undefined)
throw new Error("Refresh Token not set!");
let parameters = {
client_id: this.#apiCredentials.clientId,
client_secret: this.#apiCredentials.clientSecret,
grant_type: 'refresh_token',
refresh_token: this.#refreshToken
}
let requestOptions = {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: this.#createUrlSafeString(parameters)
}
let apiResponse = await this.requestDriveApi(this.#oauthUrl, requestOptions);
this.#accessToken = apiResponse.access_token;
}
#createUrlSafeString(parameters){
return new URLSearchParams(parameters);
}
async getUploadUrl(){
await this.#renewAccessToken();
let requestUrl = this.#baseUploadUrl + '?uploadType=resumable';
let metadata = {
'name': this.#passwordListFileName,
'parents': ['appDataFolder'],
};
let requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
'Authorization': `Bearer ${this.#accessToken}`
},
body: JSON.stringify(metadata)
}
let apiResponse = await this.requestDriveApi(requestUrl, requestOptions);
return apiResponse.headers.get('Location');
}
async getUpdateUrl(fileId){
await this.#renewAccessToken();
let requestUrl = this.#baseUploadUrl + fileId + '?uploadType=resumable';
let requestOptions = {
method: 'PATCH',
headers: {'Authorization': `Bearer ${this.#accessToken}`}
}
let apiResponse = await this.requestDriveApi(requestUrl, requestOptions);
return apiResponse.headers.get('Location');
}
async getFiles(){
await this.#renewAccessToken();
let requestUrl = this.#downloadUrl + '?spaces=appDataFolder';
let requestOptions = {
method: 'GET',
headers: {'Authorization': `Bearer ${this.#accessToken}`}
};
let apiResponse = await this.requestDriveApi(requestUrl, requestOptions);
return apiResponse.files;
}
async readFile(fileId){
await this.#renewAccessToken();
let requestUrl = this.#downloadUrl + fileId + '?alt=media'
let requestOptions = {
method: 'GET',
headers: {'Authorization': `Bearer ${this.#accessToken}`}
};
return await this.requestDriveApi(requestUrl, requestOptions);
}
async requestDriveApi(url, options){
let response = await fetch(url, options);
let jsonData;
try{
jsonData = await response.json();
}catch(err){
return response;
}
if(this.#errorExists(jsonData)){
throw new Error(this.#getErrorMessage(jsonData));
}
return jsonData;
}
setRefreshToken(refreshToken){
this.#refreshToken = refreshToken;
}
getRefreshToken(){
return this.#refreshToken;
}
#errorExists(jsonData){
if(jsonData.error)
return true;
else
return false;
}
#getErrorMessage(jsonData){
if(jsonData.error.errors)
return `${jsonData.error.code}: ${jsonData.error.errors[0].message}`;
else
return jsonData.error_description;
}
}