· 7 years ago · Apr 09, 2018, 01:10 PM
1//# sourceURL=googlepicker.js
2function googlepicker(callback) {
3 // The Browser API key obtained from the Google Developers Console.
4 // Replace with your own Browser API key, or your own key.
5 var developerKey = 'AIzaSyBsnRHkbm-A_xgLaIAjq2wIwPae7AMgflw';
6
7 // The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
8 var clientId = "777019941977-7nhmbv1tn2jq6fbto0kj7rnjqm8eoh1t.apps.googleusercontent.com";
9
10 // Replace with your own App ID. (Its the first number in your Client ID)
11 var appId = "baseline-147619";
12
13 // Scope to use to access user's Drive items.
14 var scope = ['https://www.googleapis.com/auth/drive'];
15
16 var pickerApiLoaded = false;
17 var oauthToken;
18
19 // Use the Google API Loader script to load the google.picker script.
20 function loadPicker() {
21 gapi.load('auth', { 'callback': onAuthApiLoad });
22 gapi.load('picker', { 'callback': onPickerApiLoad });
23 }
24
25 function onAuthApiLoad() {
26 window.gapi.auth.authorize(
27 {
28 'client_id': clientId,
29 'scope': scope,
30 'immediate': false
31 },
32 handleAuthResult);
33 }
34
35 function onPickerApiLoad() {
36 pickerApiLoaded = true;
37 createPicker();
38 }
39
40 function handleAuthResult(authResult) {
41 if (authResult && !authResult.error) {
42 oauthToken = authResult.access_token;
43 createPicker();
44 }
45 }
46
47 // Create and render a Picker object for searching images.
48 function createPicker() {
49 if (pickerApiLoaded && oauthToken) {
50 var view = new google.picker.View(google.picker.ViewId.DOCS);
51 var picker = new google.picker.PickerBuilder()
52 .enableFeature(google.picker.Feature.NAV_HIDDEN)
53 .setAppId(appId)
54 .setOAuthToken(oauthToken)
55 //.setOrigin(window.location.protocol + '//' + window.location.host)
56 .addView(view)
57 .addView(new google.picker.DocsView())
58 //.setDeveloperKey(developerKey)
59 .setCallback(pickerCallback)
60 //.setRelayUrl('https://' + window.location.host + '/Scripts/custom/rpc_relay.html')
61 .build();
62 picker.setVisible(true);
63 }
64 }
65
66 // A simple callback implementation.
67 function pickerCallback(data) {
68 if (data.action == google.picker.Action.PICKED) {
69 var id = data.docs[0].id;
70 var mimeType = data.docs[0].mimeType;
71 var request = new XMLHttpRequest();
72 request.open('GET', 'https://www.googleapis.com/drive/v2/files/' + id);
73 request.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token);
74 request.addEventListener('load', function () {
75 var item = JSON.parse(request.responseText);
76 var fileUrl = translateFile(item);
77 var fileName = getFileName(item);
78 console.log(item)
79 var doc = {
80 token: gapi.auth.getToken().access_token,
81 fileName: fileName,
82 uid: id,
83 thumbnail: item.thumbnailLink.replace('&sz=s220', ''),
84 icon: item.iconLink
85 };
86 callback(doc);
87
88 });
89 request.send();
90 }
91
92 }
93
94 function translateFile(data) {
95 var downloadUrl = data.downloadUrl;
96 if (typeof downloadUrl === "undefined" || downloadUrl === null) {
97 var mimeType = data.mimeType;
98 switch (mimeType) {
99 case "application/vnd.google-apps.spreadsheet":
100 downloadUrl = "https://www.googleapis.com/drive/v2/files/" + data.id + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet&key=" + developerKey;
101 break;
102 case "application/vnd.google-apps.document":
103 downloadUrl = "https://www.googleapis.com/drive/v2/files/" + data.id + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.wordprocessingml.document&key=" + developerKey;
104 //downloadUrl = data.exportLinks["application/vnd.openxmlformats-officedocument.wordprocessingml.document"] + "&key=" + developerKey;
105 break;
106 default:
107 downloadUrl = "https://www.googleapis.com/drive/v2/files/" + data.id + "/export?mimeType=text%2Fplain&key=" + developerKey;
108 //downloadUrl = data.exportLinks["text/plain"] + "&key=" + developerKey;
109 }
110 }
111 return downloadUrl;
112 }
113
114 function getFileName(data) {
115 var fileName = data.originalFilename;
116 if (typeof fileName === "undefined" || fileName === null) {
117 var mimeType = data.mimeType;
118 switch (mimeType) {
119 case "application/vnd.google-apps.spreadsheet":
120 fileName = data.title + ".xlsx";
121 break;
122 case "application/vnd.google-apps.document":
123 fileName = data.title +".docx";
124 break;
125 default:
126 fileName = data.title + ".txt"
127 }
128 }
129 return fileName;
130 }
131
132 loadPicker();
133};
134
135function googleMultiPicker(callback) {
136 // The Browser API key obtained from the Google Developers Console.
137 // Replace with your own Browser API key, or your own key.
138 var developerKey = 'AIzaSyBsnRHkbm-A_xgLaIAjq2wIwPae7AMgflw';
139
140 // The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
141 var clientId = "777019941977-7nhmbv1tn2jq6fbto0kj7rnjqm8eoh1t.apps.googleusercontent.com";
142
143 // Replace with your own App ID. (Its the first number in your Client ID)
144 var appId = "baseline-147619";
145
146 // Scope to use to access user's Drive items.
147 var scope = ['https://www.googleapis.com/auth/drive'];
148
149 var pickerApiLoaded = false;
150 var oauthToken;
151
152 // Use the Google API Loader script to load the google.picker script.
153 function loadPicker() {
154 gapi.load('auth', { 'callback': onAuthApiLoad });
155 gapi.load('picker', { 'callback': onPickerApiLoad });
156 }
157
158 function onAuthApiLoad() {
159 window.gapi.auth.authorize(
160 {
161 'client_id': clientId,
162 'scope': scope,
163 'immediate': false
164 },
165 handleAuthResult);
166 }
167
168 function onPickerApiLoad() {
169 pickerApiLoaded = true;
170 createPicker();
171 }
172
173 function handleAuthResult(authResult) {
174 if (authResult && !authResult.error) {
175 oauthToken = authResult.access_token;
176 createPicker();
177 }
178 }
179
180 // Create and render a Picker object for searching images.
181 function createPicker() {
182 if (pickerApiLoaded && oauthToken) {
183 var view = new google.picker.View(google.picker.ViewId.DOCS);
184 var picker = new google.picker.PickerBuilder()
185 .enableFeature(google.picker.Feature.NAV_HIDDEN)
186 .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
187 .setAppId(appId)
188 .setOAuthToken(oauthToken)
189 //.setOrigin(window.location.protocol + '//' + window.location.host)
190 .addView(view)
191 .addView(new google.picker.DocsView())
192 //.setDeveloperKey(developerKey)
193 .setCallback(pickerCallback)
194 //.setRelayUrl('https://' + window.location.host + '/Scripts/custom/rpc_relay.html')
195 .build();
196 picker.setVisible(true);
197 }
198 }
199
200 // A simple callback implementation.
201 function pickerCallback(data) {
202 if (data.action == google.picker.Action.PICKED) {
203
204 function loadCheck() {
205 if (files.length == data.docs.length) {
206 callback(files);
207 }
208 }
209
210 var files = [];
211
212 files.push = function () {
213 Array.prototype.push.apply(this, arguments);
214 loadCheck(this);
215 }
216
217 console.log(data);
218 data.docs.forEach(function (doc) {
219 var doc = {
220 token: gapi.auth.getToken().access_token,
221 name: getFileName(doc.title),
222 thumbnail: doc.thumbnailLink.replace('&sz=s220', ''),
223 icon: doc.iconLink,
224 size: null,
225 fileId: doc.id,
226 fileUrl: translateFile(doc)
227 };
228 files.push(doc);
229 });
230 });
231
232
233 }
234
235 }
236
237 function translateFile(data) {
238 var downloadUrl = data.downloadUrl;
239 if (typeof downloadUrl === "undefined" || downloadUrl === null) {
240 var mimeType = data.mimeType;
241 switch (mimeType) {
242 case "application/vnd.google-apps.spreadsheet":
243 downloadUrl = "https://www.googleapis.com/drive/v2/files/" + data.id + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet&key=" + developerKey;
244 break;
245 case "application/vnd.google-apps.document":
246 downloadUrl = "https://www.googleapis.com/drive/v2/files/" + data.id + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.wordprocessingml.document&key=" + developerKey;
247 //downloadUrl = data.exportLinks["application/vnd.openxmlformats-officedocument.wordprocessingml.document"] + "&key=" + developerKey;
248 break;
249 default:
250 downloadUrl = "https://www.googleapis.com/drive/v2/files/" + data.id + "/export?mimeType=text%2Fplain&key=" + developerKey;
251 //downloadUrl = data.exportLinks["text/plain"] + "&key=" + developerKey;
252 }
253 }
254 return downloadUrl;
255 }
256
257 function getFileName(data) {
258 var fileName = data.originalFilename;
259 if (typeof fileName === "undefined" || fileName === null) {
260 var mimeType = data.mimeType;
261 switch (mimeType) {
262 case "application/vnd.google-apps.spreadsheet":
263 fileName = data.title + ".xlsx";
264 break;
265 case "application/vnd.google-apps.document":
266 fileName = data.title + ".docx";
267 break;
268 default:
269 fileName = data.title + ".txt"
270 }
271 }
272 return fileName;
273 }
274
275 loadPicker();
276}