Last active
January 1, 2016 23:09
-
-
Save beriberikix/8215151 to your computer and use it in GitHub Desktop.
Sample showing Google+ Sign-In, the Google Picker & Google Drive API.
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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<!-- you can move signIn() configs into meta tags. | |
See https://developers.google.com/+/web/signin/reference#page-level_configuration_options --> | |
<title>Drive Picker & API Sample</title> | |
</head> | |
<body> | |
<!-- In a real app, you would show/hide based on the signinCallback or show a profile picture --> | |
<button id="signInButton">G+ | Sign In</button> | |
<button id="pickerButton">Pick a file</button> | |
<script type="text/javascript"> | |
// async loading | |
(function() { | |
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; | |
po.src = 'https://apis.google.com/js/picker:platform:client.js?onload=jsLoaded'; | |
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); | |
})(); | |
// register click listeners when there is a dom | |
document.addEventListener("DOMContentLoaded", function(event) { | |
// button handlers | |
var signInButton = document.querySelector('#signInButton'); | |
signInButton.addEventListener('click', signInFlow, false); | |
var pickerButton = document.querySelector('#pickerButton'); | |
pickerButton.addEventListener('click', pickerFlow, false); | |
}); | |
// various callbacks | |
var jsLoaded = function() { | |
console.log('Auth, Client & Picker JS loaded'); | |
gapi.client.load('drive', 'v2', driveClientLoaded); | |
} | |
var signinCallback = function(authResult) { | |
console.log(authResult); | |
}; | |
var driveClientLoaded = function() { | |
console.log('Drive V2 ready for action'); | |
var request = gapi.client.drive.files.list(); | |
request.execute(function(resp) { console.log(resp); }); | |
}; | |
var pickerCallback = function( 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]; | |
} | |
console.log('You picked: %s', url); | |
}; | |
// https://developers.google.com/+/web/signin/reference#javascript_api | |
var signInFlow = function() { | |
console.log('sign-in flow'); | |
gapi.auth.signIn({ | |
'clientid' : '--your-client-id', | |
'cookiepolicy' : 'single_host_origin', | |
'callback' : 'signinCallback', | |
'scope' : 'profile email https://www.googleapis.com/auth/drive.file' | |
}); | |
}; | |
var pickerFlow = function() { | |
var oauthToken = gapi.auth.getToken(); | |
var apiKey = '--your-api-key'; | |
if( oauthToken ) { | |
var picker = new google.picker.PickerBuilder(). | |
addView(google.picker.ViewId.DOCUMENTS). | |
setOAuthToken(oauthToken['access_token']). | |
setDeveloperKey(apiKey). | |
setCallback(pickerCallback). | |
build(); | |
picker.setVisible(true); | |
} else { | |
signInFlow(); | |
} | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment