· 6 years ago · Feb 11, 2020, 06:46 AM
1<!DOCTYPE html>
2<html>
3 <head>
4 <title>Drive API Quickstart</title>
5 <meta charset="utf-8" />
6 </head>
7 <body>
8 <p>Drive API Quickstart</p>
9
10 <!--Add buttons to initiate auth sequence and sign out-->
11 <button id="authorize_button" style="display: none;">Authorize</button>
12 <button id="signout_button" style="display: none;">Sign Out</button>
13
14 <pre id="content" style="white-space: pre-wrap;"></pre>
15
16 <script type="text/javascript">
17 // Client ID and API key from the Developer Console
18 var CLIENT_ID = '74003791126-e7gj29pa91mbp4pso107frf9mncrmhqc.apps.googleusercontent.com';
19 var API_KEY = 'AIzaSyCov_wX5H_P9QH_UdkhpsWGniyWQupq6OE';
20
21 // Array of API discovery doc URLs for APIs used by the quickstart
22 var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
23
24 // Authorization scopes required by the API; multiple scopes can be
25 // included, separated by spaces.
26 var SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly';
27
28 var authorizeButton = document.getElementById('authorize_button');
29 var signoutButton = document.getElementById('signout_button');
30
31 /**
32 * On load, called to load the auth2 library and API client library.
33 */
34 function handleClientLoad() {
35 gapi.load('client:auth2', initClient);
36 }
37
38 /**
39 * Initializes the API client library and sets up sign-in state
40 * listeners.
41 */
42 function initClient() {
43 gapi.client.init({
44 apiKey: API_KEY,
45 clientId: CLIENT_ID,
46 discoveryDocs: DISCOVERY_DOCS,
47 scope: SCOPES
48 }).then(function () {
49 // Listen for sign-in state changes.
50 gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
51
52 // Handle the initial sign-in state.
53 updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
54 authorizeButton.onclick = handleAuthClick;
55 signoutButton.onclick = handleSignoutClick;
56 }, function(error) {
57 appendPre(JSON.stringify(error, null, 2));
58 });
59 }
60
61 /**
62 * Called when the signed in status changes, to update the UI
63 * appropriately. After a sign-in, the API is called.
64 */
65 function updateSigninStatus(isSignedIn) {
66 if (isSignedIn) {
67 authorizeButton.style.display = 'none';
68 signoutButton.style.display = 'block';
69 listFiles();
70 } else {
71 authorizeButton.style.display = 'block';
72 signoutButton.style.display = 'none';
73 }
74 }
75
76 /**
77 * Sign in the user upon button click.
78 */
79 function handleAuthClick(event) {
80 gapi.auth2.getAuthInstance().signIn();
81 }
82
83 /**
84 * Sign out the user upon button click.
85 */
86 function handleSignoutClick(event) {
87 gapi.auth2.getAuthInstance().signOut();
88 }
89
90 /**
91 * Append a pre element to the body containing the given message
92 * as its text node. Used to display the results of the API call.
93 *
94 * @param {string} message Text to be placed in pre element.
95 */
96 function appendPre(message) {
97 var pre = document.getElementById('content');
98 var textContent = document.createTextNode(message + '\n');
99 pre.appendChild(textContent);
100 }
101
102 /**
103 * Print files.
104 */
105 function listFiles() {
106 gapi.client.drive.files.list({
107 'pageSize': 10,
108 'fields': "nextPageToken, files(id, name)"
109 }).then(function(response) {
110 appendPre('Files:');
111 var files = response.result.files;
112 if (files && files.length > 0) {
113 for (var i = 0; i < files.length; i++) {
114 var file = files[i];
115 appendPre(file.name + ' (' + file.id + ')');
116 }
117 } else {
118 appendPre('No files found.');
119 }
120 });
121 }
122
123 </script>
124
125 <script async defer src="https://apis.google.com/js/api.js"
126 onload="this.onload=function(){};handleClientLoad()"
127 onreadystatechange="if (this.readyState === 'complete') this.onload()">
128 </script>
129 </body>
130</html>