从Google云端硬盘启动Appmaker文档批准

时间:2018-06-22 11:28:13

标签: google-app-maker

我已根据需要在Appmaker中自定义了文档管理系统模板。现在,我不想提供每次启动App的功能,而是想从Google Drive启动工作流。因此,用户可以直接从Google Drive选择要批准的文件。

我的问题是,是否有任何Rest呼叫或通过第三方应用程序可以启动DMS工作流程的东西?

1 个答案:

答案 0 :(得分:1)

好吧,我找到了实现结果的出路。

步骤:

  1. Drive API提供了一种在Google云端硬盘的“打开方式”菜单中添加应用的方法。
  2. 因此,我创建了我的自定义应用并进行了部署。该应用程序将仅从Google云端硬盘“打开方式”菜单中接收参数,并将其传递给Appmaker文档批准系统。
  3. 在Appmaker创建请求页面中,如果请求包含这些参数,则进行解析,如果是,则使用这些参数选择文件。
  4. 通过这种方式,我的用户可以从Google云端硬盘启动文档批准工作流程。

参考文献:

  1. How to add/list your App in Google Drive

  2. Step by Step video guideline to Create and publish your App

代码:

  1. “打开方式”应用代码,用于将用户从Google云端硬盘重定向到Appmaker。

code.gs:

var AUTHORIZE_URL = 'https://accounts.google.com/o/oauth2/auth'; 
var TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'; 

var REDIRECT_URL= ScriptApp.getService().getUrl();
var tokenPropertyName = 'GOOGLE_OAUTH_TOKEN'; 

var CLIENT_ID = 'your client id';
var CLIENT_SECRET = 'your client secrect';

function doGet(e) {
  var HTMLToOutput;

  if(e.parameters.state){
    var state = JSON.parse(e.parameters.state);
    if(state.action === 'create'){
      HTMLToOutput = "<html><h1>Creation Of Docs Not supported by this App!</h1></html>"; 
    }
    else {
      var id = state.exportIds;
       var file = DriveApp.getFileById(id);
       //append params to your appmaker URL
      var url = 'yourappmaker published url'+'?param1='+file.getName()+'&param2='+file.getUrl()+'#AddRequest';
      HTMLToOutput = HtmlService.createHtmlOutput('<html><script>'
  +'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
  +'var a = document.createElement("a"); a.href="'+url+'"; a.target="_blank";'
  +'if(document.createEvent){'
  +'  var event=document.createEvent("MouseEvents");'
  +'  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
  +'  event.initEvent("click",true,true); a.dispatchEvent(event);'
  +'}else{ a.click() }'
  +'close();'
  +'</script>'
  // Offer URL as clickable link in case above code fails.
  +'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
  +'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
  +'</html>')
  .setWidth( 90 ).setHeight( 1 );
    }
  }
  else if(e.parameters.code){//if we get "code" as a parameter in, then this is a callback. we can make this more explicit
    getAndStoreAccessToken(e.parameters.code);
    HTMLToOutput = '<html><h1>App is installed, you can close this window now or navigate to your <a href="https://drive.google.com">Google Drive</a>.</h1></html>';
  }
  else {//we are starting from scratch or resetting
    HTMLToOutput = "<html><h1>Install this App into your Google Drive!</h1><a href='"+getURLForAuthorization()+"'>click here to start</a></html>";
  }
  console.log(getURLForAuthorization());
  return HtmlService.createHtmlOutput(HTMLToOutput);
}

function getURLForAuthorization(){
  return AUTHORIZE_URL + '?response_type=code&client_id='+CLIENT_ID+'&redirect_uri='+REDIRECT_URL +
    '&scope=https://www.googleapis.com/auth/drive.install https://www.googleapis.com/auth/userinfo.email';  
}

function getAndStoreAccessToken(code){
  var parameters = { method : 'post',
                    payload : 'client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&grant_type=authorization_code&redirect_uri='+REDIRECT_URL+'&code=' + code};

  var response = UrlFetchApp.fetch(TOKEN_URL,parameters).getContentText();   
  var tokenResponse = JSON.parse(response);
  UserProperties.setProperty(tokenPropertyName, tokenResponse.access_token);
}

function getUrlFetchOptions() {
  return {'contentType' : 'application/json',
          'headers' : {'Authorization' : 'Bearer ' + UserProperties.getProperty(tokenPropertyName),
                       'Accept' : 'application/json'}};
}

//naive check, not using for now, use refresh tokens and add proper checking
function isTokenValid() {
  return UserProperties.getProperty(tokenPropertyName);
}
  1. 在“文档”工作流程的“创建请求”页面中,将事件添加到onAttach()方法中。在函数下面编写

//客户端

function checkIfRedirected(widget)
{
//   console.log(location.origin);  
  google.script.url.getLocation(function(location) {
  var params = location.parameter;
  var param1 = params.param1;
    var param2 = params.param2;
    widget.datasource.item.DocumentName = param1;
    widget.datasource.item.DocumentUrl = param2;    
    widget.datasource.item.Owner = app.user.email;
  });
}