-
Notifications
You must be signed in to change notification settings - Fork 1
/
drive.js
90 lines (78 loc) · 2.67 KB
/
drive.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
//******************** ********************/
//******************** GLOBAL VARIABLES ********************/
var CLIENT_ID = "534902264735-kqorq3i2c7hl2tg2s8h40kbhgmde9fo3.apps.googleusercontent.com";
var API_KEY = "AIzaSyBaYqW1LaG3Oua5aT40u6AqmaasNVPkwe0";
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
var SCOPES = "https://www.googleapis.com/auth/drive.metadata.readonly";
var authorizeButton = document.getElementById("authorize_button");
var signoutButton = document.getElementById("signout_button");
//******************** END OF GLOBAL VARIABLES ********************/
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));
}
);
}
//end of initializing **************************************************************************************
/**
* Called when the signed in status changes, to update the UI
* appropriately. After a sign-in, the API is called.
*/
//user signed in *************************************************
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.color = "none";
signoutButton.style.display = "block";
listFiles();
} 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);
}