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
You can use the Apps Script API to get the script's "deployment 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.
Each project has a list of deployments. First get a deployment, and then get the deployment Id from the JSON object.
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"]
}
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.
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 first run the code, review the error messages. For example, look at the 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"
}
]
}
]
}
}
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;
}
List Project Deployements
Key words: Apps Script, project ID, deployment ID, Apps Script API, published URL