Google Drive: File, Download
Imports any file in Google Drive as Workflow data. It is also possible to change the file name when importing the file. If a Google file (Docs/Sheets/Slides, etc.) is specified, an error will occur (use GFile Export addon instead).
Configs
  • U: Select HTTP_Authz Setting *
  • A1: Set File-ID to Download *#{EL}
  • B1: Set File Name if you want to save as a different name#{EL}
  • B2: Select FILE DATA that stores Downloaded File (append) *
  • B3: Select STRING DATA that stores File Name in Drive (update)
  • B4: Select STRING DATA that stores Mime-Type of File (update)
Script (click to open)
// GraalJS Script (engine type: 2)

//////// START "main()" /////////////////////////////////////////////////////////////////

main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthzSetting     = configs.get      ( "AuthzConfU" );   /// REQUIRED
  engine.log( " AutomatedTask Config: Authz Setting: " + strAuthzSetting );
const strInputfileId      = configs.get      ( "StrConfA1" );    /// REQUIRED
  if( strInputfileId    === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A1: FileID} is empty \n" );
  }
let   strDownloadSaveas   = configs.get      ( "StrConfB1" );    // NotRequired
  if( strDownloadSaveas === "" ){
    engine.log( " AutomatedTask ConfigWarning:" +
                " Config {B1: DownloadSaveas} is empty" );
  }
const filesPocketDownload  = configs.getObject( "SelectConfB2" ); /// REQUIRED
const strPocketNameOnDrive = configs.getObject( "SelectConfB3" ); // NotRequired
const strPocketMimetype    = configs.getObject( "SelectConfB4" ); // NotRequired


//// == Data Retrieving / ワークフローデータの参照 ==
let filesAttached = engine.findData( filesPocketDownload ); // java.util.ArrayList
if( filesAttached === null ) {
  engine.log( " AutomatedTask FilesArray {B2} (empty)" );
  filesAttached = new java.util.ArrayList();
}else{
  engine.log( " AutomatedTask FilesArray {B2}: " +
              filesAttached.size() + " files" );
}


//// == Calculating / 演算 ==
/// Gets a file's metadata by ID.
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/reference/files/get
// request1, prepare
let request1Uri = "https://www.googleapis.com/drive/v3/files/" + strInputfileId;
let request1    = httpClient.begin(); // HttpRequestWrapper
    request1    = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
    // https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
    request1    = request1.queryParam( "supportsAllDrives", "true" );
// request1, try
const response1     = request1.get( request1Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + request1Uri );
const response1Code = response1.getStatusCode() + "";
const response1Body = response1.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response1Code );
if( response1Code !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    response1Code + "\n" + response1Body + "\n" );
}
// response1, parse
/* engine.log( response1Body ); // debug
{
  "kind": "drive#file",
  "id": "0BwSW_lixFpUuQ2JyV1pqU3VzTGs",
  "name": "Questetra-Intro-en.pdf",
  "mimeType": "application/pdf"
}
*/
const response1Obj = JSON.parse( response1Body );
engine.log( " AutomatedTask ApiResponse: File Name OnDrive: " + response1Obj.name );
engine.log( " AutomatedTask ApiResponse: File MIME-Type: " + response1Obj.mimeType );
if( strDownloadSaveas === "" ){
  strDownloadSaveas   = response1Obj.name;
}

/// Download a file stored on Google Drive.
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/manage-downloads#download_a_file_stored_on_google_drive
// request2, prepare
let request2Uri = "https://www.googleapis.com/drive/v3/files/" + strInputfileId;
let request2    = httpClient.begin(); // HttpRequestWrapper
    request2    = request2.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
    // https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
    request2    = request2.queryParam( "supportsAllDrives", "true" );
    request2    = request2.queryParam( "alt", "media" ); // a download of content is being requested.
// request2, try
const response2     = request2.get( request2Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest2 Start: " + request2Uri );
const response2Code = response2.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response2Code );
if( response2Code !== "200"){
  const response2Body = response2.getResponseAsString() + "";
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    response2Code + "\n" + response2Body + "\n" );
}
// response2, parse
const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
                 strDownloadSaveas, response2.getContentType(), response2.getResponse()
                );
filesAttached.add( fileTmp );


//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( filesPocketDownload,    filesAttached );

if( strPocketNameOnDrive !== null ){
  engine.setData( strPocketNameOnDrive, response1Obj.name );
}
if( strPocketMimetype    !== null ){
  engine.setData( strPocketMimetype,    response1Obj.mimeType );
}

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

/*
Notes:
- The File ID can be obtained from the sharing settings screen.
    - Refer to the file ID of the image from the URL in "Open in new window"

Notes-ja:
- File ID は共有設定画面等から取得できます。
    - 画像ファイルのIDは「新しいウィンドウで開く」のURLを参照します。

APPENDIX-en
- Setting example of "HTTP Authentication" (OAuth2)
    - 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, Consumer Secret:
        - ( from https://console.developers.google.com/ )
        - Redirect URLs: https://s.questetra.net/oauth2callback

APPENDIX-ja
- "HTTP認証"(OAuth2)の設定例
    - 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, Consumer Secret:
        - ( from https://console.developers.google.com/ )
        - Redirect URLs: https://s.questetra.net/oauth2callback
*/

Download

2021-04-08 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-file-download-2021/
The Addon-import feature is available with Professional or Enterprise edition.

Notes

  • The File ID can be obtained from the sharing settings screen.
    • Refer to the file ID of the image from the URL in “Open in new window”

Capture

Imports any file in Google Drive as Workflow data. It is also possible to change the file name. If a Google file (Docs/Sheets/Slides, etc.) is specified, an error will occur (use GFile Export addon instead).

Appendix

See also

3 thoughts on “Google Drive: File, Download”

  1. Pingback: Google Drive: File; Download – Questetra Support

  2. Pingback: Google Drive: GFile, Export as Text – Questetra Support

  3. Pingback: Google Drive: File, Convert – Questetra Support

Leave a Reply

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

%d bloggers like this: