从文件URL查找应用程序脚本已发布的U​​RL-通过已发布的Web应用程序URL搜索文件 [英] Find apps script published URL from file URL - search files by published Web App URL

查看:62
本文介绍了从文件URL查找应用程序脚本已发布的U​​RL-通过已发布的Web应用程序URL搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们从客户以前的开发人员那里继承了许多Google Apps脚本项目. Apps脚本通过嵌入式窗口小部件部署在Google网站(sites.google.com)上的各个页面上.每当我们需要处理其中之一时,我们就可以通过以下方式找到项目:

We inherited quite a few Google Apps Script projects from our client's previous developer. The Apps Scripts are deployed on Google sites (sites.google.com) to various pages through embedded widgets. Whenever we have needed to work on one of them, we have been able to locate the project by:

  1. 转到sites.google.com上包含小部件的页面,
  2. 编辑窗口小部件,
  3. 注意到发布的网址,
  4. 要访问script.google.com,
  5. 打开/编辑名为类似于"我们想要的项目的项目,
  6. 点击发布>部署为网络应用
  7. 将当前Web应用程序URL"与上面第3步中看到的内容进行比较

这是一个相当乏味的过程,但是到目前为止已经完成了.

That's a rather tedious process, but it has worked up to this point.

其中一个小工具在通过sites.google.com进行访问时开始显示需要授权..."消息,因此我们需要跟踪它所属的项目.我们完成了上面的步骤1-3,但是找不到任何具有与小工具相匹配的URL的项目.

One of the gadgets started showing an "Authorization is required..." message when visited through sites.google.com, so we need to track down the project to which it belongs. We get through steps 1-3 (above), but we cannot find any projects that have a URL that matches the gadget.

我的直觉是组织内部的其他人(而不是开发人员的帐户)拥有该项目,但可能是5或6个不同的人,他们都不是开发人员,或者不是专门从事技术工作的人.另一种可能性是,开发人员帐户拥有,但该项目的名称很差,我不高兴要经过5到7遍步骤数十次才能找到它.

My hunch is that somebody else inside of the organization (not the developer's account) owns the project, but it could be 5 or 6 different people, and none of them are developers or are particularly technically-minded. The other possibility is that the developer account does own the project, but it is poorly named, and I am not excited to go through steps 5-7 dozens of times to find it.

是否可以根据其URL定位特定项目? script.google.com上的搜索工具似乎仅搜索项目名称,不幸的是,在这种情况下,这没有帮助.

Is there a way to locate a specific project based on its URL? The search tool at script.google.com seems to only search for project names, which is, unfortunately, not helpful in this case.

推荐答案

您可以使用Apps脚本API获取脚本的部署ID".

You can use the Apps Script API to get the script's "deployment ID."

首先,您需要使用DriveApp来获取Apps Script项目文件的列表.然后,您需要遍历所有文件,获取文件ID,然后使用文件ID获取部署信息.

First you need to use DriveApp to get a list of Apps Script project files. Then you need to loop through all the files, get the file ID, and use the file ID to get the deployment information.

每个项目都有一个部署列表.首先获取部署,然后从JSON对象获取部署ID.

Each project has a list of deployments. First get a deployment, and then get the deployment Id from the JSON object.

要以我概述的方式使用Apps Script API,必须在appsscript.json清单文件中设置所需的作用域.

To use the Apps Script API in the way that I am outlining, you must set the required scopes in the appsscript.json manifest file.

以下是设置外观的示例:

Here is an example of how the settings should look:

{
  "timeZone": "America/New_York",
  "dependencies": {
  },
  "webapp": {
    "access": "ANYONE",
    "executeAs": "USER_ACCESSING"
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://www.googleapis.com/auth/script.projects", 
                  "https://www.googleapis.com/auth/drive.scripts", 
                  "https://www.googleapis.com/auth/drive", 
                  "https://www.googleapis.com/auth/script.container.ui", 
                  "https://www.googleapis.com/auth/script.external_request", 
                  "https://www.googleapis.com/auth/script.scriptapp",
                  "https://www.googleapis.com/auth/script.deployments",
                  "https://www.googleapis.com/auth/script.deployments.readonly"]
}

首次运行代码时,会提示您授权权限.但是,即使在授予权限后,您仍然需要转到开发者控制台并为该项目启用Apps Script API.

When you run the code for the first time, you'll get a prompt to authorize the permissions. But even after you authorize the permissions, you'll still need to go to the developers console and enable the Apps Script API for the project.

因此,您将在一个项目中运行代码以获取所有Apps脚本文件的列表,然后获取每个项目的部署,并从部署中获取部署ID.

So, you'll be running code from one project to get a list of all the Apps Script files, and then get the deployments of each project, and from the deployments get the deployment ID.

运行代码时,使用查看"菜单并选择日志".您会在日志中看到如下错误消息:

When you run the code, use the View menu and choose Logs. You'll see an error message in the log that looks like this:

[18-06-22 08:51:32:841 EDT] response: {
  "error": {
    "code": 403,
    "message": "Apps Script API has not been used in project abc123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/script.googleapis.com/overview?project=abc123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console API activation",
            "url": "https://console.developers.google.com/apis/api/script.googleapis.com/overview?project=abc123"
          }
        ]
      }
    ]
  }
}

将Url复制到开发人员仪表板,然后将其粘贴到浏览器地址栏中.在信息中心中,启用API.

Copy out the Url to the developer dashboard, and then paste it into your browsers address bar. In the dashboard, enable the API.

以下是您需要使用的代码示例:

Here is a sample of the code you need to use:

function searchForProjectWithCertainID() {
  var files,params,projectID_toFind,rtrn,thisFileID;
  
  projectID_toFind = "Put ID to find here";
  
  //params = 'mimeType contains "json"';
  //files = DriveApp.searchFiles(params);
  
  files = DriveApp.getFilesByType(MimeType.GOOGLE_APPS_SCRIPT);//Get all Apps Script files
  
  while (files.hasNext()) {
    thisFileID = files.next().getId();
    
    //Logger.log(thisFileID)
    
    rtrn = getDeploymentID(thisFileID);
    
    if (rtrn === projectID_toFind) {
      break;
    }
  }
}

function getDeploymentID(scriptId) {

  var errMsg,L,options,response,theAccessTkn,url;

  theAccessTkn = ScriptApp.getOAuthToken();
  
  url = "https://script.googleapis.com/v1/projects/" + scriptId + "/deployments";

  options = {
    "method" : "GET",
    "muteHttpExceptions": true,
    "headers": {
      'Authorization': 'Bearer ' +  theAccessTkn
    }
  };

  response = UrlFetchApp.fetch(url,options);
  Logger.log('response: ' + response)

  response = JSON.parse(response);//The response must be parsed into JSON even though it is an object
  
  L = response.deployments.length;
  //Logger.log('response.deployments.length: ' + response.deployments.length)
  
  if (typeof response === 'object') {
    errMsg = response.error;
    if (errMsg) {
      errMsg = errMsg.message;
      return 'err' + errMsg;
    }
  }
  
  //Logger.log(response.deployments[L - 1].deploymentId);
  
  return response.deployments[L - 1].deploymentId;
}

列出项目部署

关键字:Apps脚本,项目ID,部署ID,Apps Script API,发布的URL

Key words: Apps Script, project ID, deployment ID, Apps Script API, published URL

这篇关于从文件URL查找应用程序脚本已发布的U​​RL-通过已发布的Web应用程序URL搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆