-
Notifications
You must be signed in to change notification settings - Fork 8
/
testingGCloud.html
263 lines (226 loc) · 7.08 KB
/
testingGCloud.html
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!DOCTYPE html>
<html>
<head>
<title>Drive API Quickstart</title>
<meta charset="utf-8" />
</head>
<body>
<p>Drive API Quickstart</p>
<!--Add buttons to initiate auth sequence and sign out-->
<button id="authorize_button" style="display: none;">Authorize</button>
<button id="signout_button" style="display: none;">Sign Out</button>
<pre id="content" style="white-space: pre-wrap;"></pre>
<script type="text/javascript">
// Client ID and API key from the Developer Console
var CLIENT_ID = '992438477515-45fu2fovp17921micdfsnhcl8pn0fjbd.apps.googleusercontent.com';
var API_KEY = 'AIzaSyCZaipoQKvSrgNdQUZL_0Bc98SDG_Okcvs';
// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = 'https://www.googleapis.com/auth/drive.file';
var authorizeButton = document.getElementById('authorize_button');
var signoutButton = document.getElementById('signout_button');
/**
* On load, called to load the auth2 library and API client library.
*/
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
/**
* Initializes the API client library and sets up sign-in state
* listeners.
*/
function initClient() {
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
}, function(error) {
appendPre(JSON.stringify(error, null, 2));
});
}
/**
* Called when the signed in status changes, to update the UI
* appropriately. After a sign-in, the API is called.
*/
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
listFiles();
// createFolder();
// createFile();
// getFile()
// patchFile();
} else {
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
}
}
/**
* Sign in the user upon button click.
*/
function handleAuthClick(event) {
gapi.auth2.getAuthInstance().signIn();
}
/**
* Sign out the user upon button click.
*/
function handleSignoutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}
/**
* Append a pre element to the body containing the given message
* as its text node. Used to display the results of the API call.
*
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
var pre = document.getElementById('content');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
function createFolder() {
console.log("HELLO");
var fileMetadata = {
'name': 'SatisGraphtoryTestFolder',
'mimeType': 'application/vnd.google-apps.folder'
};
gapi.client.drive.files.create({
resource: fileMetadata,
fields: 'id'
}).then(function(response) {
console.log(response);
});
}
/**
* Print files.
*/
function listFiles() {
gapi.client.drive.files.list({
'pageSize': 10,
'fields': "nextPageToken, files(id, name, trashed)"
}).then(function(response) {
appendPre('Files:');
var files = response.result.files;
var numFiles = 0;
if (files && files.length > 0) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file.trashed) continue;
numFiles++;
appendPre(file.name + ' (' + file.id + ')');
}
}
if (numFiles === 0) {
appendPre('No files found.');
}
});
}
function getFile() {
// https://drive.google.com/file/d/1Q38O2J4NkVzMfOyIop6CoD5zxBR8Qu-f/view?usp=drivesdk
// gapi.client.drive.files.get({
// fileId: '1lGYf7HTF2jqVp2PNV_yocRRiX3uK9Csj',
// alt: 'media',
// }).then(response => console.log(response.body))
fetch("https://www.googleapis.com/drive/v3/files/1parVWzimSkABuaUrS_TB9e-iaKOHsnIX?key=AIzaSyCZaipoQKvSrgNdQUZL_0Bc98SDG_Okcvs&alt=media")
.then(resp => resp.json()).then(resp => console.log(resp))
// gapi.client.request({
// path: `/drive/v2/files/${fileId}`,
// method: 'GET',
// }).then((response) => {
// console.log(response);
// });
}
function patchFile() {
let fileId = "1parVWzimSkABuaUrS_TB9e-iaKOHsnIX";
gapi.client.drive.files.update({
fileId,
uploadType: 'media',
mimeType: 'application/json',
supportsAllDrives: true
}).then(function(response) {
console.log(response)
})
// gapi.client.request({
// path: `/upload/drive/v3/files/${fileId}`,
// method: 'PATCH',
// params: {
// uploadType: 'media',
// mimeType: 'application/json',
// supportsAllDrives: true
// },
// body: JSON.stringify({
// modified: true
// }),
// }).then((response) => {
// if (response.status !== 200) {
// console.error(`client.request: ${response}`);
// }
// }, (error) => {
// console.error(JSON.stringify(error, null, 2));
// });
}
function createFile() {
// 1aMLp6N8OOtH2bwpcPEyFhklFGGKOBdI6
console.log("GGGGG")
var fileMetadata = {
name: 'lwm_config.json',
description: 'Saved config for the Last War Manager',
mimeType: 'application/json',
uploadType: 'multipart',
};
gapi.client.drive.files.create({
resource: fileMetadata,
fields: 'id,name,webViewLink',
supportsAllDrives: true
}).then(function(response) {
console.log("FIle found:", response);
var fileId = response.result.id
var permissions =
{
'type': 'anyone',
'role': 'reader'
};
console.log("WAHOO");
gapi.client.drive.permissions.create({
resource: permissions,
fileId: fileId,
fields: 'id',
}).then(perms => {
console.log(perms);
})
gapi.client.request({
path: `/upload/drive/v3/files/${fileId}`,
method: 'PATCH',
params: {
uploadType: 'media',
mimeType: 'application/json',
},
body: JSON.stringify({}),
}).then((response) => {
// console.log(response);
if (response.status !== 200) {
console.error(`client.request: ${response}`);
}
}, (error) => {
console.error(JSON.stringify(error, null, 2));
});
});
}
</script>
<script async defer src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>
</body>
</html>