Select a Google Drive file with picker

The use of Google picker to select a file is very easy, documentation is here. One problem I have encountered is related with authorization.

For asking for permisson to select a file (with picker) you need the access_token provided in the client side. But then, you may need to process the file in server side, so you probably need to send the access_token to the server.

We have seen that passing access_token from client to the server is not secure (at least you have to do with https), and Google recommends Sign-In server side flow as documented here.

In order to do that, you need the one-time code for exchanging in server side for an access_token. The only way to do that with javascript libraries is by getting permission from the user to access the specified scopes offline. Thus can be done by the regular sing-in functions:

Summarizing, we need both in client side: access_token (for the picker), code (for the server processing of the file).

Instead, if we use advanced Google Sign-In methods, we can use:

This way, we can get both tokens at one time. But this is not the recomended way if you plan to use authorization more than one time.

In the recommended way, you should authorize twice: for the initial sig-in (for get the access_token) and for the offline access (to get the exchangable code).

The tricky, in order to be asked only once for the authorization is the use of reloadAuthResponse().

The code how I resolved is here:

file: picker1.html

<html lang="en">
		<script src="" async defer></script>		
		<script src=""></script>
		<button id="signOut" style="float:left; margin-right: 10px; display:hidden;">Sign Out</button>
		<div id="GoogleID" style="padding-top: 2px;">Connecting ...</div>
		<button id="pickFileButton" style="height: 35px; float: left; margin-right: 10px;">
			<img src="Google_Drive_Logo.svg" style="width:28px; height:28px; float:left;">
			<p style="display: inline-block; margin-top: 8px; margin-left: 8px; margin-right: 5px;">Select File</p>
		<div id="fileID" style="padding-top: 10px">Not file selected</div>
		var GoogleAuth;
		var googleUser;
		var access_token;
		var id_token;
		var code_token;
		var clientID = '';
		function start() {			
			gapi.load('auth2', function() {						
				auth2 = gapi.auth2.init({
					client_id: clientID,
					// Scopes to request in addition to 'profile' and 'email'
					scope: ''
				}).then(function() {
					GoogleAuth = gapi.auth2.getAuthInstance();
					if (GoogleAuth.isSignedIn.get()) {		//User is signed						
						googleUser = GoogleAuth.currentUser.get();						
						//access_token = googleUser.getAuthResponse(true).access_token;						
						$('#GoogleID').html('Connected as: ' + googleUser.getBasicProfile().getName());
					} else {	// User is not signed in. Start Google auth flow when click button
						$('#GoogleID').html('Not connected');
		function updateSigninStatus() {
			console.log('Status changed');
		$('#signOut').click(function() {			
			GoogleAuth.disconnect().then(function () {
				$('#GoogleID').html('Not connected');
				$('#fileID').html('Not file selected');
		$('#pickFileButton').click(function() {		
			if (typeof GoogleAuth === 'undefined') {     // the variable is undefined which implies auth2 has not been loaded
				console.log('Error: auth2 has not been loaded');
				function(resp) {
					code = resp.code;						
					googleUser = GoogleAuth.currentUser.get();
					$('#GoogleID').html('Connected as: ' + googleUser.getBasicProfile().getName());
						function(authResponse) {								
							access_token = authResponse.access_token;
							id_token = authResponse.id_token;								
		function createPicker() {		
			console.log('code: '+code);
			console.log('access_token: '+access_token);
			console.log('id_token: '+id_token);
			gapi.load('picker', function() {				
				var picker = new google.picker.PickerBuilder().
		function pickerCallback(data) {			
			var url = 'nothing';
			if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
				var doc = data[google.picker.Response.DOCUMENTS][0];
				fileUrl = doc[google.picker.Document.URL];
				fileId = doc[google.picker.Document.ID];				
				fileName = doc[google.picker.Document.NAME];
				$('#fileID').html('File: ' + fileName);				
				var data = 'code='+code+'&fileId='+fileId;
					type: 'POST',
					url: '',
					// Always include an `X-Requested-With` header in every AJAX request to protect against CSRF attacks.
					headers: {'X-Requested-With': 'XMLHttpRequest'},
					contentType: 'application/octet-stream; charset=utf-8',
					processData: false,
					data: data,
					//dataType: "json"
				}).done(function(result) {					
					console.log('exchanged access_token: '+result);
					// Handle or verify the server response.

In the server it must be exchanged the code for an access_token. The code is here:

file: picker1.php

ini_set('display_errors', 'On');

$code = urlencode(explode("=",explode("&", file_get_contents("php://input"))[0])[1]);
$fileId = explode("=",explode("&", file_get_contents("php://input"))[1])[1];

$client_id = '';
$client_secret = 'HotSFRmLEfrt3vvn971uftnh';

$url = '';
$redirect_uri = '';
$data = array(
	'code' => $code,
	'client_id' => $client_id,
	'client_secret' => $client_secret,
	'redirect_uri' => $redirect_uri,
	'grant_type' => 'authorization_code'						
$content = urldecode(http_build_query($data));	
//You should get the string in the same form than: $content = 'code='.$code.'&client_id='.$client_id.'&client_secret='.$client_secret.'&redirect_uri='.$redirect_uri.'&grant_type=authorization_code';
$headers = array(
	'Content-Type: application/x-www-form-urlencoded',
	'Content-Type: application/json',
	'Content-Length: '.strlen($content)

// use key 'http' even if you send the request to https://...
$options = array(
	'http' => array(		
		'header'  => $headers,	// When php is compiled --with-curlwrappers
		//'header'  => implode('\r\n', $headers).'\r\n',	// // When php is not compiled --with-curlwrappers
		'method'  => 'POST',
		'content' => $content
$context  = stream_context_create($options);
$result = json_decode(file_get_contents($url, false, $context));

// If you want to do the same call with cURL
$ch = curl_init();
	//set the url, number of POST vars, POST data
	curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch,CURLOPT_CUSTOMREQUEST, "POST");
	//execute post
	$result = json_decode(curl_exec($ch));
	var_dump ($result);

$access_token = $result->access_token;
echo $access_token;




Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *