List Files from a Google Drive using the GDrive API

Post Reply
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

List Files from a Google Drive using the GDrive API

Post by bkromer »

How can I list all files in a specific folder of a Google Drive over the Google Drive API?
I spent hours reading the documentation of the Drive API the Enfocus Code-Samples and trying stuff, I just don't get it. :(
https://developers.google.com/drive/api/v2/reference

In the Enfocus Docs at the very bottom of the HTTP-class description is an example of how to grant access, Upload and delete files from GDrive.
https://www.enfocus.com/manuals/Develop ... class.html
But I don't get it. :?:

So far I enabled the API, created an API-Key and also created an APP with Client ID and Token in the Developer Console.
https://console.developers.google.com/apis/dashboard

I tried to adapt the code of the Enfocus Documentation but I don't even get any server-responses. (i run this on the scripter, not on the server!)

Has anybody successfully built something upon the gdrive API?

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var theHTTP = new HTTP(  HTTP.SSL );
	theHTTP.authScheme = HTTP.OauthAuth; 
	theHTTP.authorization = "Bearer 12312332asdasdasdadsasd"; 
	theHTTP.url = "https://www.googleapis.com/drive/v2/files/"
	function ErrorExists( inMessage : String ) : bool
{
	var theError = inMessage.find( "error" );
	
	return theError >= 0;
}

function RefreshToken( job : Job ) : String
{
	var theHTTP = new HTTP( HTTP.SSL );

	theHTTP.addParameter( "refresh_token", "REFRESH_TOKEN" ); // 
	theHTTP.addParameter( "client_id", "aas6dasdadasd-12313323a6sd5a65ds.apps.googleusercontent.com" ); //"CLIENT_ID"
	theHTTP.addParameter( "client_secret","asdasdWWSwasda213123tAVGa"  ); // "CLIENT_SECRET"
	theHTTP.addParameter( "grant_type", "refresh_token" );
	
	theHTTP.url = "https://accounts.google.com/o/oauth2/token";
	
	theHTTP.post();
	
	while( !theHTTP.waitForFinished( 3 ) )
	{
		job.log( 1, "Refresh token in progress" );
	}
	
	var theFinishedStatus = theHTTP.finishedStatus; // Statuses: Ok, Failed, Interrupted
	var theServerResponse = theHTTP.getServerResponse().toString();
	
	if( theFinishedStatus == HTTP.Ok && !ErrorExists( theServerResponse ) )
	{		
		var theNewTokenWithQuotes = theServerResponse.split( "," )[0].split( ":" )[1];
		var theNewToken = theNewTokenWithQuotes.substring( 2, theNewTokenWithQuotes.length - 1 );
		
		job.log( 1, "The new token with quotes: %1", theNewTokenWithQuotes );
		job.log( 1, "The new token: %1", theNewToken );
		job.log( 1, "Refresh token is successfully" );
		
		return theNewToken;
	}
	else if( theFinishedStatus == HTTP.Failed || ErrorExists( theServerResponse ) )
	{	
		job.log( 1, "Server response: %1", theServerResponse );
		job.log( 3, "Refresh token failed with the error: %1", theHTTP.lastError );
	}
	else
	{
		job.log( 2, "Unknown error" );
	}
}

function jobArrived( s : Switch, job : Job )
{
	var NewAccessToken = RefreshToken( job );
	
	var theHTTP = new HTTP( HTTP.SSL );
	
	theHTTP.authScheme = HTTP.OauthAuth;
	theHTTP.authorization = "Bearer " + NewAccessToken;	
	theHTTP.setAttachedFile( job.getPath() );
	theHTTP.url = "https://www.googleapis.com/upload/drive/v2/files";
	
	// send file to Google Drive
	theHTTP.post();
	
	while( !theHTTP.waitForFinished( 3 ) )
	{
		job.log( 1, "Upload file to Google Drive in progress" );
	}
	
	var theFinishedStatus = theHTTP.finishedStatus;
	var theServerResponse = theHTTP.getServerResponse().toString();
	
	if( theFinishedStatus == HTTP.Ok && !ErrorExists( theServerResponse ) )
	{
		job.log( 1, "Server response: %1", theServerResponse );
		job.log( 1, "Upload file to Google Drive completed successfully" );
	}
	else if( theFinishedStatus == HTTP.Failed || ErrorExists( theServerResponse ) )
	{	
		job.log( 1, "Server response: %1", theServerResponse );
		job.log( 3, "Upload file to Google Drive failed with the error: %1", theHTTP.lastError );
	}
	else
	{
		job.log( 2, "Unknown error" );
	}
	
	// get file ID
	var theFileIdWithQuotes = theServerResponse.split( "," )[1].split( ":" )[1];
	var theFileId = theFileIdWithQuotes.substring( 2, theFileIdWithQuotes.length - 1 );
	
	theHTTP.url = "https://www.googleapis.com/drive/v2/files/" + theFileId;
	
	// delete file from Google Drive by fileId
	theHTTP.del();
	
	while( !theHTTP.waitForFinished( 3 ) )
	{
		job.log( 1, "Delete file from Google Drive in progress" );
	}
	
	theFinishedStatus = theHTTP.finishedStatus;
	theServerResponse = theHTTP.getServerResponse().toString();
	
	if( theFinishedStatus == HTTP.Ok && !ErrorExists( theServerResponse ) )
	{
		job.log( 1, "Delete file from Google Drive is successfully" );
		
		job.sendToSingle( job.getPath() );
	}
	else if( theFinishedStatus == HTTP.Failed || ErrorExists( theServerResponse ) )
	{	
		job.log( 1, "Server response: %1", theServerResponse );
		job.log( 3, "Delete file from Google Drive failed with the error: %1", theHTTP.lastError );
	}
	else
	{
		job.log( 2, "Unknown error" );
	}
	
}
}
Benjamin
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Re: List Files from a Google Drive using the GDrive API

Post by dkelly »

I'm confused because your code contains 2 jobArrived functions.

In the code below you aren't calling a HTTP function for your API call to /files endpoint. According to documentation is uses the GET verb. In the other jobArrived() you were calling POST not GET.

Code: Select all

	var theHTTP = new HTTP(  HTTP.SSL );
	theHTTP.authScheme = HTTP.OauthAuth; 
	theHTTP.authorization = "Bearer 12312332asdasdasdadsasd"; 
	theHTTP.url = "https://www.googleapis.com/drive/v2/files/"
	theHTTP.get();
	
	while( !theHTTP.waitForFinished( 3 ) ) {
		job.log( 1, "Call to Google Drive in progress" );
	}
	
	var theFinishedStatus = theHTTP.finishedStatus;
	var theServerResponse = theHTTP.getServerResponse().toString();
	
	....rest of code...
}
Post Reply