Chris Ostmo
Chris Ostmo

Reputation: 1252

Find apps script published URL from file URL - search files by published Web App URL

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. Going to the page at sites.google.com that contains the widget,
  2. Editing the widget,
  3. Noting the published URL,
  4. Going to script.google.com,
  5. Open/edit projects that are named "like" what we are looking for,
  6. Click Publish > Deploy as web app
  7. Compare "Current web app URL" to what is seen in step 3 above

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

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.

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.

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.

Upvotes: 2

Views: 2419

Answers (1)

Alan Wells
Alan Wells

Reputation: 31300

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

Upvotes: 6

Related Questions