Google Drive Upload

Uploads the file specified by File type data item to the specified directory of Google Drive, and stores its communication log in a String type data item
Google Drive Simple Uploads
GoogleDrive-simpleUpload-20171127-addonxml
(md5:372b89966fb220f00791003932653ccc)

    • B’: File(s) stored in the Data Item selected at config B
    • C’: Google Drive folder ID stored in the Data Item selected at config C*
    • Group of files of B’ will be uploaded to the specified directory C’
    • The IDs of the uploaded files are stored in the data item selected at D (comma separated)
    • Communication log will be stored in the Data Item selected at config X
    • G Suite Users should consider about using [Service Task (Google Drive)] (M229)
    • It requires registration of Project at Google API Manager in advance
    • If specified folder ID does not exist, it results in an error
    • If there are filename conflicts, multiple files with same filename will be saved
    • * https://drive.google.com/drive/folders/0BexampleEXAMPLEexampledvamc
    • Version 20171127 is “Team Drive” compatible version

    A. Set OAuth2 Config Name (at [OAuth 2.0 Setting])Single-line inputRequired
    B. Select FILE DATA for Upload FileData selectFileRequired
    C. Select STRING/SELECT DATA for Folder IDData selectString(single line)orSelect(radio/select/search)Required
    D. Select STRING DATA for File IDs (update)Data selectString(single line)
    X. Select STRING DATA for Access Log (update)Data selectString(multiple line)
    -variable namevariable label
    Aconf_OAuth2Set OAuth2 Config Name (at [OAuth 2.0 Setting])
    Bconf_DataIdBSelect FILE DATA for Upload File
    Cconf_DataIdCSelect STRING/SELECT DATA for Folder ID
    Dconf_DataIdDSelect STRING DATA for File IDs (update)
    Xconf_DataIdXSelect STRING DATA for Access Log (update)
    // Google Drive Simple Uploads (ver. 20171127)
    // (c) 2017, Questetra, Inc. (the MIT License)
    // by Google Drive APIs Files get
    // https://developers.google.com/drive/v3/web/simple-upload
    // https://developers.google.com/drive/v3/reference/files/update
    
    // OAuth2 config sample at [OAuth 2.0 Setting]
    // - 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" or "https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/drive.metadata"
    // - Consumer Key: (Get by Google Developers Console)
    // - Consumer Secret: (Get by Google Developers Console)
    
    
    //// == Config Retrieving / 工程コンフィグの参照 ==
    var oauth2 = configs.get( "conf_OAuth2" ) + "";
    var dataIdB = configs.get( "conf_DataIdB" ) + "";
    var dataIdC = configs.get( "conf_DataIdC" ) + "";
    var dataIdD = configs.get( "conf_DataIdD" ) + "";
    var dataIdX = configs.get( "conf_DataIdX" ) + "";
    // convert 'java.lang.String' to 'javascript string'
    
    
    //// == Data Retrieving / ワークフローデータの参照 ==
    var uploadFiles = engine.findDataByNumber( dataIdB );
    // java.util.ArrayList 
    if (uploadFiles === null) {
      throw new Error("No Files attached in this Process");
    } // error
    var folderId = "";
    if( engine.findDataDefinitionByNumber( dataIdC ).matchDataType( "SELECT_SINGLE" ) ){
        folderId = engine.findDataByNumber( dataIdC ).get(0).getValue() + "";
    }else{
        folderId = engine.findDataByNumber( dataIdC ) + "";
    }
    
    
    //// == Calculating / 演算 ==
    var fileIds = "";
    var accessLog = "";
    var token  = httpClient.getOAuth2Token( oauth2 );
    var numOfFiles = uploadFiles.size() - 0;
    
    for( var i = 0; i < numOfFiles; i++  ){
      var uri = "https://www.googleapis.com/upload/drive/v3/files";
      var response = httpClient.begin()
        .bearer( token )
        .queryParam( "uploadType", "media" )
        .body( uploadFiles.get(i) )
        .post( uri );
      accessLog += "---POST request--- " + response.getStatusCode() + "\n";
      accessLog += response.getResponseAsString() + "\n";
      var responseObj = JSON.parse( response.getResponseAsString() );
    
      var uri2 = "https://www.googleapis.com/drive/v3/files/";
        uri2 += responseObj.id;
      fileIds += responseObj.id;
      if( i !== numOfFiles - 1 ){ fileIds += ","; }
      var request2Obj = {};
        request2Obj.name = uploadFiles.get(i).getName() + "";
      var response2 = httpClient.begin()
        .bearer( token )
        .queryParam( "addParents", folderId )
        .queryParam( "removeParents", "root" )
        .queryParam( "supportsTeamDrives", true )
        .body( JSON.stringify( request2Obj ), "application/json" )
        .patch( uri2 );
      accessLog += "---PATCH request--- " + response2.getStatusCode() + "\n";
      accessLog += response2.getResponseAsString() + "\n";
    }
    
    
    //// == Data Updating / ワークフローデータへの代入 ==
    if ( dataIdD !== "" ){ engine.setDataByNumber( dataIdD, fileIds ); }
    if ( dataIdX !== "" ){ engine.setDataByNumber( dataIdX, accessLog ); }