forked from iRaul/minimal-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilepicker.js
57 lines (51 loc) · 1.65 KB
/
filepicker.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
// The Browser API key obtained from the Google Developers Console.
var developerKey = 'AIzaSyDzR0gTVm3WFHXQmEl1bUxehr3L1CmlmhM';
// The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
var clientId = "272434584361-13ac1lmsqg4esrn1mnuagpso0obgjl3v.apps.googleusercontent.com";
// Scope to use to access user's photos.
var scope = 'https://www.googleapis.com/auth/drive';
var pickerApiLoaded = false;
var oauthToken;
// Use the API Loader script to load google.picker and gapi.auth.
function onApiLoad() {
gapi.load('auth2', onAuthApiLoad);
gapi.load('picker', onPickerApiLoad);
}
function onAuthApiLoad() {
gapi.auth2.authorize({
client_id: clientId,
scope: scope
}, handleAuthResult);
}
function onPickerApiLoad() {
pickerApiLoaded = true;
createPicker();
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
createPicker();
}
}
// Create and render a Picker object for picking user Photos.
function createPicker() {
if (pickerApiLoaded && oauthToken) {
var picker = new google.picker.PickerBuilder().
addView(google.picker.ViewId.DOCUMENTS).
setOAuthToken(oauthToken).
setDeveloperKey(developerKey).
setCallback(pickerCallback).
build();
picker.setVisible(true);
}
}
// A simple callback implementation.
function pickerCallback(data) {
var url = 'nothing';
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
url = doc[google.picker.Document.URL];
}
var message = 'You picked: ' + url;
console.log(message);
}