首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Google Drive启动Appmaker文档审批

从Google Drive启动Appmaker文档审批
EN

Stack Overflow用户
提问于 2018-06-22 19:28:13
回答 1查看 236关注 0票数 0

我已经根据需要在Appmaker中定制了文档管理系统模板。现在,我希望提供从谷歌Drive.So启动工作流程的功能,而不是每次都去Appmaker启动审批,用户可以直接从Google Drive选择要审批的文件。

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

EN

回答 1

Stack Overflow用户

发布于 2018-08-03 12:38:47

好吧,我找到了一种方法来实现这个结果。

步骤:

  1. 驱动应用程序接口提供了一种方法,可以将你的应用程序添加到Drive.
    1. 的“打开方式”菜单中,所以我创建了我的自定义应用程序并部署了它。此应用程序只需从谷歌硬盘的“打开方式”菜单中接收参数,并将其传递到Appmaker文档审批系统。Appmaker request页面中的
    2. 解析请求是否包含这些参数,如果包含这些参数,则使用这些参数选择文件。
    3. 这样,我的用户就可以从谷歌硬盘启动文档审批工作流。

参考:

  1. How to add/list your App in Google Drive
  2. Step by Step video guideline to Create and publish your App

代码:

  1. “打开方式”应用程序代码,用于将用户从Google Drive重定向到Appmaker。

code.gs:

代码语言:javascript
复制
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. 在文档工作流“Create Request”页面中,将事件添加到onAttach()方法。在下面写下函数,

//客户端

代码语言:javascript
复制
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;
  });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50986952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档