Google Drive: File; Download

Google Drive: File; Download

Downloads the specified file in the Drive to the Workflow Data Item. If a Google File (Docs / Sheets / Slides, etc.) is specified, an error will occur (Please use File Export). The File ID can be obtained from the sharing settings screen.

2019-09-10 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-file-download/

Configs
  • A: Select OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set FILE-ID to Download * #{EL}
  • C: Select FILES DATA for Downloaded File (added) *
  • D: Set FILE NAME if you want to save as a different name #{EL}
  • E: Select STRING DATA for File Name in Drive (update)
  • F: Select STRING DATA for Mime-Type of File (update)
Script
// (c) 2019, Questetra, Inc. (the MIT License)

//// == OAuth2 Setting example ==
// Authorization Endpoint URL:
//  "https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force"
// Token Endpoint URL:
//  "https://accounts.google.com/o/oauth2/token"
// Scope:
//  "https://www.googleapis.com/auth/drive.readonly"
// Client ID:
//  ( from https://console.developers.google.com/ )
// Consumer Secret:
//  ( from https://console.developers.google.com/ )
//  *Redirect URLs: "https://s.questetra.net/oauth2callback"


//////// START "main()" ////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2  = configs.get( "conf_OAuth2"  ) + "";
const fileId  = configs.get( "conf_FileId") + "";
const dataIdC = configs.get( "conf_DataIdC" ) + "";
let   saveAs  = configs.get( "conf_SaveAs") + "";
const dataIdE = configs.get( "conf_DataIdE" ) + "";
const dataIdF = configs.get( "conf_DataIdF" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'
engine.log( " AutomatedTask Config: File ID: " + fileId );
if( fileId === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" + 
                   " String {B} (File ID) is required \n" );
}
engine.log( " AutomatedTask Config: Save As: " + saveAs );


//// == Data Retrieving / ワークフローデータの参照 ==
var tmpFiles = engine.findDataByNumber( dataIdC ); // java.util.ArrayList
if( tmpFiles === null ) {
  engine.log( " AutomatedTask FilesArray {C} (empty)" );
  tmpFiles = new java.util.ArrayList();
}else{
  engine.log( " AutomatedTask FilesArray {C}: " + 
              tmpFiles.size() + " files" );
}


//// == Calculating / 演算 ==
/// obtain OAuth2 Access Token
const token   = httpClient.getOAuth2Token( oauth2 );

/// get File Name (Metadata)
let apiRequest = httpClient.begin(); // HttpRequestWrapper
apiRequest = apiRequest.bearer( token );
apiRequest = apiRequest.queryParam( "supportsAllDrives", "true" );
// This parameter will only be effective until June 1, 2020.
// see https://developers.google.com/drive/api/v3/reference/files/get
const apiUri = "https://www.googleapis.com/drive/v3/files/" + fileId;
engine.log( " AutomatedTask Trying: GET " + apiUri );
const response = apiRequest.get( apiUri );
const responseCode = response.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode );
if( responseCode !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
         responseCode + "\n" + response.getResponseAsString() + "\n" );
}
const responseStr = response.getResponseAsString() + "";
const responseObj = JSON.parse( responseStr );
engine.log( " AutomatedTask ApiResponse: File Name: " + responseObj.name );
engine.log( " AutomatedTask ApiResponse: File MIME Type: " + responseObj.mimeType );
if( saveAs === "" ){
  saveAs = responseObj.name;
}

/// get Media File
let apiRequest2 = httpClient.begin(); // HttpRequestWrapper
apiRequest2 = apiRequest2.bearer( token );
apiRequest2 = apiRequest2.queryParam( "supportsAllDrives", "true" );
apiRequest2 = apiRequest2.queryParam( "alt", "media" );
const apiUri2 = "https://www.googleapis.com/drive/v3/files/" + fileId;
engine.log( " AutomatedTask Trying: GET " + apiUri2 );
const response2 = apiRequest2.get( apiUri2 );
const responseCode2 = response2.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode2 );
if( responseCode2 !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
         responseCode2 + "\n" + response2.getResponseAsString() + "\n" );
}
const qfile = new com.questetra.bpms.core.event.scripttask.NewQfile(
    saveAs, response2.getContentType(), response2.getResponse()
  );
tmpFiles.add( qfile );


//// == Data Updating / ワークフローデータへの代入 ==
engine.setDataByNumber( dataIdC, tmpFiles );
if( dataIdE !== "" ){
  engine.setDataByNumber( dataIdE, responseObj.name );
}
if( dataIdF !== "" ){
  engine.setDataByNumber( dataIdF, responseObj.mimeType );
}

} //////// END "main()" ////////

Download

Capture

Notes

  1. Supports all drives such as My Drive and Shared Drive.
  2. If a Google File (Docs / Sheets / Slides, etc.) is specified, an error “fileNotDownloadable” will occur.
  3. fileNotDownloadable: Only files with binary content can be downloaded. Use Export with Google Docs files.

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: