Chr*_*tmo 2 url google-sites google-apps google-apps-script
我们从客户以前的开发人员那里继承了很多 Google Apps 脚本项目。Apps 脚本通过嵌入的小部件部署在 Google 站点 (sites.google.com) 上的各种页面上。每当我们需要处理其中之一时,我们都能够通过以下方式找到该项目:
这是一个相当乏味的过程,但到目前为止它已经奏效了。
当通过sites.google.com 访问时,其中一个小工具开始显示“需要授权...”消息,因此我们需要追踪它所属的项目。我们完成了步骤 1-3(上面),但是我们找不到任何具有与小工具匹配的 URL 的项目。
我的预感是组织内部的其他人(不是开发人员的帐户)拥有该项目,但可能是 5 或 6 个不同的人,他们都不是开发人员或特别具有技术头脑。另一种可能是开发者账户确实拥有该项目,但它的名字很糟糕,我并不兴奋,要经过 5-7 几十次步骤才能找到它。
有没有办法根据 URL 定位特定项目?script.google.com 上的搜索工具似乎只搜索项目名称,不幸的是,这在这种情况下没有帮助。
您可以使用 Apps Script API 来获取脚本的“部署 ID”。
首先,您需要使用DriveApp
来获取 Apps 脚本项目文件的列表。然后需要遍历所有文件,获取文件ID,通过文件ID获取部署信息。
每个项目都有一个部署列表。首先获取部署,然后从 JSON 对象中获取部署 Id。
要以我概述的方式使用 Apps Script API,您必须在 appsscript.json 清单文件中设置所需的范围。
以下是设置外观的示例:
{
"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"]
}
Run Code Online (Sandbox Code Playgroud)
首次运行代码时,会提示您授权权限。但即使您授权了权限,您仍然需要转到开发人员控制台并为项目启用 Apps Script API。
因此,您将从一个项目运行代码以获取所有 Apps 脚本文件的列表,然后获取每个项目的部署,并从部署中获取部署 ID。
首次运行代码时,请查看错误消息。例如,查看日志。您将在日志中看到如下所示的错误消息:
[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"
}
]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
将 Url 复制到开发人员仪表板,然后将其粘贴到浏览器地址栏中。在仪表板中,启用 API。
以下是您需要使用的代码示例:
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;
}
Run Code Online (Sandbox Code Playgroud)
关键词:Apps Script,项目ID,部署ID,Apps Script API,发布的URL