Skip to content

Instantly share code, notes, and snippets.

@oshliaer
Last active January 19, 2017 06:07
The simple file demonstrates #google_js_client_api #gapi
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TEST</title>
<script type="text/javascript" src="./js.js"></script>
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</head>
<body>
<input type="button" id="authorize-button" onchange="handleAuthClick" value="AUTH" style="visibility: hidden;">
<div id="code_sample" style="visibility: hidden;">
<pre>Insert the code below in console</pre>
<hr></hr>
<xmp>
gapi.client.request({
path : '/drive/v2/files',
method : 'get',
params : {
'q' : "mimeType = 'application/vnd.google-apps.folder' and trashed=false and 'me' in owners"
}
}).then(function(e){console.log(e.result.items)})
</xmp>
<hr></hr>
<pre id="info"></pre>
</div>
</body>
</html>
var clientId = '314159265359.apps.googleusercontent.com';
var scopes = [ 'https://www.googleapis.com/auth/drive', 'profile' ];
function handleClientLoad() {
window.setTimeout(checkAuth, 1);
}
function checkAuth() {
gapi.auth.authorize({
client_id : clientId,
scope : scopes,
immediate : true
}, handleAuthResult);
}
function handleAuthResult(authResult) {
var authorizeButton = document.getElementById('authorize-button');
var code_sample = document.getElementById('code_sample');
if (authResult && !authResult.error) {
var expires_in = (authResult.expires_in - 5 * 600) * 1000;
document.getElementById('info').innerHTML += new Date() + ' Timer has been set for checkAuth() at ' + expires_in/1000/60 + ' min.<br>';
setTimeout(checkAuth, expires_in);
authorizeButton.style.visibility = 'hidden';
code_sample.style.visibility = '';
} else {
authorizeButton.style.visibility = '';
code_sample.style.visibility = 'hidden';
authorizeButton.onclick = handleAuthClick;
}
}
function handleAuthClick(event) {
gapi.auth.authorize({
client_id : clientId,
scope : scopes,
immediate : false
}, handleAuthResult);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment