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" );
}
}
}