Created
June 12, 2016 14:41
-
-
Save cevek/71e724f92d70a654bfc439b1b75c2506 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript"> | |
// Your Client ID can be retrieved from your project in the Google | |
// Developer Console, https://console.developers.google.com | |
var CLIENT_ID = '469780497507-emil5hs8k93ro3fnpqqe8guvejsrj884.apps.googleusercontent.com'; | |
var SCOPES = [/*'https://www.googleapis.com/auth/drive', */'https://www.googleapis.com/auth/drive.appfolder', 'https://www.googleapis.com/auth/drive.file']; | |
/** | |
* Check if current user has authorized this application. | |
*/ | |
function checkAuth() { | |
gapi.auth.authorize( | |
{ | |
'client_id': CLIENT_ID, | |
'scope': SCOPES.join(' '), | |
'immediate': true | |
}, handleAuthResult); | |
} | |
/** | |
* Handle response from authorization server. | |
* | |
* @param {Object} authResult Authorization result. | |
*/ | |
function handleAuthResult(authResult) { | |
var authorizeDiv = document.getElementById('authorize-div'); | |
if (authResult && !authResult.error) { | |
// Hide auth UI, then load client library. | |
authorizeDiv.style.display = 'none'; | |
loadDriveApi(); | |
} else { | |
// Show auth UI, allowing the user to initiate authorization by | |
// clicking authorize button. | |
authorizeDiv.style.display = 'inline'; | |
} | |
} | |
/** | |
* Initiate auth flow in response to user clicking authorize button. | |
* | |
* @param {Event} event Button click event. | |
*/ | |
function handleAuthClick(event) { | |
gapi.auth.authorize( | |
{client_id: CLIENT_ID, scope: SCOPES, immediate: false}, | |
handleAuthResult); | |
return false; | |
} | |
/** | |
* Load Drive API client library. | |
*/ | |
function loadDriveApi() { | |
gapi.client.load('drive', 'v3', listFiles); | |
} | |
/** | |
* Print files. | |
*/ | |
function listFiles() { | |
var request = gapi.client.drive.files.list({ | |
// spaces: 'appDataFolder', | |
'pageSize': 100, | |
'fields': "nextPageToken, files(id, name)" | |
}); | |
const button = document.createElement('button'); | |
document.body.appendChild(button); | |
button.textContent = 'create something'; | |
button.onclick = function () { | |
gapi.client.drive.files.create({ | |
name: "hello boy", | |
mimeType: 'text/plain', | |
// parents: ['appDataFolder'], | |
fields: 'id, name' | |
}).execute(function (resp) { | |
console.log(arguments); | |
gapi.client.request({ | |
'path': '/upload/drive/v3/files/' + resp.id + '?uploadType=media', | |
'method': 'PATCH', | |
'body': "Hello everybody" | |
}).execute(function (result) { | |
console.log('upload', result); | |
}); | |
}); | |
} | |
request.execute(function (resp) { | |
console.log(resp); | |
appendPre('Files:'); | |
var files = resp.files; | |
if (files && files.length > 0) { | |
gapi.client.request({ | |
'path': '/drive/v3/files/' + "0B8kdvUt14xcqYWhaN3RqTnJtdEk" + '?alt=media', 'method': 'GET'}).then(data=>console.log('file', data)); | |
for (var i = 0; i < files.length; i++) { | |
var file = files[i]; | |
appendPre(file.name + ' (' + file.id + ')'); | |
} | |
} else { | |
appendPre('No files found.'); | |
} | |
}); | |
} | |
/** | |
* Append a pre element to the body containing the given message | |
* as its text node. | |
* | |
* @param {string} message Text to be placed in pre element. | |
*/ | |
function appendPre(message) { | |
var pre = document.getElementById('output'); | |
var textContent = document.createTextNode(message + '\n'); | |
pre.appendChild(textContent); | |
} | |
</script> | |
<script src="https://apis.google.com/js/client.js?onload=checkAuth"> | |
</script> | |
</head> | |
<body> | |
<div id="authorize-div" style="display: none"> | |
<span>Authorize access to Drive API</span> | |
<!--Button for the user to click to initiate auth sequence --> | |
<button id="authorize-button" onclick="handleAuthClick(event)"> | |
Authorize | |
</button> | |
</div> | |
<pre id="output"></pre> | |
</body> | |
</html> |
It just copy paste from google drive api docs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Can you please license this code of yours?
Thanks.