Skip to content

Instantly share code, notes, and snippets.

@JacobHsu
Last active August 29, 2015 14:05
Show Gist options
  • Save JacobHsu/a2b63f4e58489a9be28d to your computer and use it in GitHub Desktop.
Save JacobHsu/a2b63f4e58489a9be28d to your computer and use it in GitHub Desktop.
#Google Drive - Google Picker with Drive
<script>
var clientId = '720409271749-hqv3lb4u0q82t68vperem5oi3kosskol.apps.googleusercontent.com';
var developerKey = 'AIzaSyBTEWsJ4aXdoOzB4ey81eX9-ja7HejL4Qc';
var accessToken;
function onApiLoad() {
gapi.load('auth', authenticateWithGoogle);
gapi.load('picker');
}
function authenticateWithGoogle() {
window.gapi.auth.authorize({
'client_id': clientId,
'scope': ['https://www.googleapis.com/auth/drive']
}, handleAuthentication);
}
function handleAuthentication(result) {
if(result && !result.error) {
accessToken = result.access_token;
setupPicker();
}
}
function setupPicker() {
var picker = new google.picker.PickerBuilder()
.setOAuthToken(accessToken)
.setDeveloperKey(developerKey)
.addView(new google.picker.DocsUploadView())
.setCallback(myCallback)
.build();
picker.setVisible(true);
}
function myCallback(data) {
if (data.action == google.picker.Action.PICKED) {
alert(data.docs[0].name);
} else if (data.action == google.picker.Action.CANCEL) {
alert('goodbye');
}
}
</script>
<script src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment